Hi!
Question: I want to know if a string value is numeric only. How can I do this?
Its very easy with Vertica to test if a string value is a number:
daniel=> select to_number('0x0abc');
to_number
-----------
2748
(1 row)
but a problem, that it throws exception on non-numeric values:
daniel=> select to_number('1 egg');
ERROR 2826: Could not convert "1 egg" to a float8
and thats why you can't use it with CASE clause.
So I created UDF Scalar function - is_numeric, that do it:
daniel=> select str, DECODE(is_numeric(str), 't', 'TRUE', 'f', 'FALSE') from nums ;
str | case
-----------+-------
0X0DEAD | TRUE
0x0abc | TRUE
1 foo bar | FALSE
1P10 | FALSE <----- fail
1e10 | TRUE
1p10 | FALSE <----- fail
40 | TRUE
45.0 | TRUE
999.999 | TRUE
INFINITY | TRUE
Infinity | TRUE
NAN | TRUE
bar | FALSE
egg 4 | FALSE
iNf | TRUE
nAn | TRUE
(16 rows)
FYA: As you can see, my function fails on 1 case - 1P10(1024 in decimal representation).
Tested on Vertica 7,8,9.
Source code here.
thanks.