Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

check if "it's a number" function in Oracle

I'm trying to check if a value from a column in an oracle (10g) query is a number in order to compare it. Something like:

select case when ( is_number(myTable.id) and (myTable.id >0) )              then 'Is a number greater than 0'              else 'it is not a number'         end as valuetype     from table myTable 

Any ideas on how to check that?

like image 371
Fgblanch Avatar asked Feb 22 '11 18:02

Fgblanch


People also ask

Is number function in Oracle?

The Oracle numeric functions take a numeric input as an expression and return numeric values. The return type for most of the numeric functions is NUMBER. Calculates the absolute value of an expression. Calculates the angle value (in radians) of a specified cosine.

How do you check if a string is a number Oracle?

Answer: To test a string for numeric characters, you could use a combination of the LENGTH function, TRIM function, and TRANSLATE function built into Oracle. The string value that you are testing.

How do you check if a number is a whole number in Oracle?

To check if the value is an integer you can use the trunc function. This will take the decimal values off. If it's a numeric column, there is also if mod(value,1) != 0... , which works great for me.

How can you tell if a value is not numeric in Oracle?

IF(option_id = 0021) THEN IF((value<10000) or (value>7200000) or /* Numeric Check */)THEN ip_msg(6214,option_name); -- Error Message return; END IF; END IF; In SQL Server, I simply used ISNUMERIC() .


1 Answers

One additional idea, mentioned here is to use a regular expression to check:

SELECT  foo  FROM    bar WHERE   REGEXP_LIKE (foo,'^[[:digit:]]+$'); 

The nice part is you do not need a separate PL/SQL function. The potentially problematic part is that a regular expression may not be the most efficient method for a large number of rows.

like image 158
Saish Avatar answered Oct 21 '22 14:10

Saish