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?
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.
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.
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.
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() .
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With