Wednesday, May 23, 2012

The Correct ISNUMERIC Function in Oracle

1. In the function, I use REPLACE(UPPER()) to replace the 'E' with 'A', which fixes the issue with strings like '1234e2', otherwise Oracle considers it as a number, i.e. 123400
2. I prefer to return NUMBER instead of BOOLEAN, which allows me to use the function in DML SQL statements directly.
-----------------------------------------------------------------------------
FUNCTION ISNUMERIC (This_str IN VARCHAR2) RETURN NUMBER
IS
  v_dummy NUMBER;
BEGIN
  v_dummy := TO_NUMBER(REPLACE(UPPER(This_str),'E','A'));
  RETURN 0;
  EXCEPTION WHEN OTHERS THEN
    RETURN 1;
END;
/