Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

I have the following code that returns an error message if my value is invalid. I would like to give the same error message if the value given is not numeric.

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(). I would like to do something similar in Oracle. Such as,

IF((!ISNUMERIC(value)) or (value<10000) or (value>7200000))     THEN ... 
like image 826
Kyle Williamson Avatar asked Mar 02 '15 21:03

Kyle Williamson


People also ask

How do you check if a string is numeric in 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 column is numeric or not?

The ISNUMERIC() function tests whether an expression is numeric. This function returns 1 if the expression is numeric, otherwise it returns 0.

Is Numeric in Oracle function?

The position of "ISNUMERIC(attribute2)=1" does not guarantee that it will be evaluated first. You might also consider making a version of isnumeric that return the numeric value if the argument is a NUMBER, and returns NULL if the argument is not a NUMBER.

Can we use NVL for number in Oracle?

What is the Oracle equivalent of NVL for number datatypes? This has nothing to do with NVL . You can't do select 42 from dual union select 'Arthur' from dual either. A column must have the same datatype in each and every row.


2 Answers

REGEXP_LIKE(column, '^[[:digit:]]+$') 

returns TRUE if column holds only numeric characters

like image 148
Rob van Laarhoven Avatar answered Oct 05 '22 12:10

Rob van Laarhoven


From Oracle DB 12c Release 2 you could use VALIDATE_CONVERSION function:

VALIDATE_CONVERSION determines whether expr can be converted to the specified data type. If expr can be successfully converted, then this function returns 1; otherwise, this function returns 0. If expr evaluates to null, then this function returns 1. If an error occurs while evaluating expr, then this function returns the error.

 IF (VALIDATE_CONVERSION(value AS NUMBER) = 1) THEN      ...  END IF; 

db<>fiddle demo

like image 44
Lukasz Szozda Avatar answered Oct 05 '22 13:10

Lukasz Szozda