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 ...
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.
The ISNUMERIC() function tests whether an expression is numeric. This function returns 1 if the expression is numeric, otherwise it returns 0.
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.
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.
REGEXP_LIKE(column, '^[[:digit:]]+$')
returns TRUE if column holds only numeric characters
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
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