How can I determine if a string is numeric in SQL?

前端 未结 7 1582
难免孤独
难免孤独 2020-12-03 17:51

In a SQL query on Oracle 10g, I need to determine whether a string is numeric or not. How can I do this?

相关标签:
7条回答
  • 2020-12-03 18:25

    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.

    0 讨论(0)
提交回复
热议问题