ISNUMERIC Function?

Moderator: NorbertKrupa

Post Reply
User avatar
Josh
Intermediate
Intermediate
Posts: 106
Joined: Thu Jan 26, 2012 9:38 pm

ISNUMERIC Function?

Post by Josh » Fri Apr 13, 2012 8:15 pm

Hi. SQL Server has a function named ISNUMERIC that can be used to determine whether an expression is a valid numeric type. Is there a function like this in Vertica?
Thank you!
Joshua

User avatar
JimKnicely
Site Admin
Site Admin
Posts: 1825
Joined: Sat Jan 21, 2012 4:58 am
Contact:

Re: ISNUMERIC Function?

Post by JimKnicely » Mon Apr 16, 2012 2:26 pm

Hi Josh,

There isn't a built in function like ISNUMERIC, but you can create your own.

Example:

Code: Select all

dbadmin=> create function isnumeric(x varchar) return varchar
dbadmin-> as
dbadmin-> begin
dbadmin->   return REGEXP_COUNT(x, '^[0-9.-]+$');
dbadmin->
dbadmin-> end;
CREATE FUNCTION
dbadmin=> select col1, isnumeric(col1) isnumeric from test;
 col1 | isnumeric
------+-----------
 100  | 1
 25.5 | 1
 A    | 0
 25A  | 0
 -60  | 1
(5 rows)
Have fun!
Jim Knicely

Image

Note: I work for Vertica. My views, opinions, and thoughts expressed here do not represent those of my employer.

rajasekhart
Intermediate
Intermediate
Posts: 87
Joined: Wed Apr 25, 2012 8:10 am

Re: ISNUMERIC Function?

Post by rajasekhart » Thu Apr 26, 2012 6:12 am

Hi Knicely87,

Thats the great way of thinking.
It is very helpful to me.

Thanks a lot
Rajasekhar.T|HP ATP Vertica Big Data Solutions V1

sgwaltney3
Newbie
Newbie
Posts: 1
Joined: Thu Jan 29, 2015 10:07 pm

Re: ISNUMERIC Function?

Post by sgwaltney3 » Thu Jan 29, 2015 10:50 pm

The ISNUMERIC function written above is not entirely accurate.

It will identify non-numeric strings as numeric and numeric strings as non-numeric.

Example: '....'
This is not a numeric string and will not be recognized as such by Vertica (ie CAST('....' as DOUBLE PRECISION)).
However the regular expression will match this.

Example: '+1.98E2'
Vertica supports an optional plus ("+") or minus sign ("-") as well as decimal exponents. Therefore, this is a valid numeric string with the value of 198.
However the regular expression will not match this.

To accurately identify strings Vertica will accept as DOUBLE PRECISION, the regex should be more like '^[-\+]?([0-9]+.?[0-9]*|[0-9]*.?[0-9]+)([Ee][-+]?[0-9]+)?$'.

But even this does not take into consideration that Vertica also accepts hexadecimal numbers, infinity, NAN and null values.

Post Reply

Return to “Vertica SQL”