How can you tell if a value is not numeric in Oracle?
There is no built-in function. You could write one
CREATE FUNCTION is_numeric( p_str IN VARCHAR2 )
RETURN NUMBER
IS
l_num NUMBER;
BEGIN
l_num := to_number( p_str );
RETURN 1;
EXCEPTION
WHEN value_error
THEN
RETURN 0;
END;
and/or
CREATE FUNCTION my_to_number( p_str IN VARCHAR2 )
RETURN NUMBER
IS
l_num NUMBER;
BEGIN
l_num := to_number( p_str );
RETURN l_num;
EXCEPTION
WHEN value_error
THEN
RETURN NULL;
END;
You can then do
IF( is_numeric( str ) = 1 AND
my_to_number( str ) >= 1000 AND
my_to_number( str ) <= 7000 )
If you happen to be using Oracle 12.2 or later, there are enhancements to the to_number
function that you could leverage
IF( to_number( str default null on conversion error ) >= 1000 AND
to_number( str default null on conversion error ) <= 7000 )
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
REGEXP_LIKE(column, '^[[:digit:]]+$')
returns TRUE if column holds only numeric characters