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 ...
The best answer I found on internet:
You can use the following regular expression which will match integers (e.g.,
123
), floating-point numbers (12.3
), and numbers with exponents (1.2e3
):If you want to accept
+
signs as well as-
signs (as Oracle does withTO_NUMBER()
), you can change each occurrence of-
above to[+-]
. So you might rewrite your block of code above as follows:I am not altogether certain that would handle all values so you may want to add an
EXCEPTION
block or write a customto_number()
function as @JustinCave suggests.There is no built-in function. You could write one
and/or
You can then do
If you happen to be using Oracle 12.2 or later, there are enhancements to the
to_number
function that you could leverageFrom
Oracle DB 12c Release 2
you could use VALIDATE_CONVERSION function:db<>fiddle demo
returns TRUE if column holds only numeric characters