ISNUMERIC Function?
Moderator: NorbertKrupa
ISNUMERIC Function?
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
Joshua
- JimKnicely
- Site Admin
- Posts: 1825
- Joined: Sat Jan 21, 2012 4:58 am
- Contact:
Re: ISNUMERIC Function?
Hi Josh,
There isn't a built in function like ISNUMERIC, but you can create your own.
Example:
Have fun!
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)
Jim Knicely
Note: I work for Vertica. My views, opinions, and thoughts expressed here do not represent those of my employer.
Note: I work for Vertica. My views, opinions, and thoughts expressed here do not represent those of my employer.
-
- Intermediate
- Posts: 87
- Joined: Wed Apr 25, 2012 8:10 am
Re: ISNUMERIC Function?
Hi Knicely87,
Thats the great way of thinking.
It is very helpful to me.
Thanks a lot
Thats the great way of thinking.
It is very helpful to me.
Thanks a lot
Rajasekhar.T|HP ATP Vertica Big Data Solutions V1
-
- Newbie
- Posts: 1
- Joined: Thu Jan 29, 2015 10:07 pm
Re: ISNUMERIC Function?
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.
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.