In a SQL query on Oracle 10g, I need to determine whether a string is numeric or not. How can I do this?
I don't have access to a 10G instance for testing, but this works in 9i:
CREATE OR REPLACE FUNCTION is_numeric (p_val VARCHAR2) RETURN NUMBER IS v_val NUMBER; BEGIN BEGIN IF p_val IS NULL OR TRIM (p_val) = '' THEN RETURN 0; END IF; SELECT TO_NUMBER (p_val) INTO v_val FROM DUAL; RETURN 1; EXCEPTION WHEN OTHERS THEN RETURN 0; END; END; SELECT is_numeric ('333.5') is_numeric FROM DUAL;
I have assumed you want nulls/empties treated as FALSE.