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

前端 未结 7 1609
难免孤独
难免孤独 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:22

    I found that the solution

    LENGTH(TRIM(TRANSLATE(string1, ' +-.0123456789', ' '))) is null
    

    allows embedded blanks ... it accepts "123 45 6789" which for my purpose is not a number.

    Another level of trim/translate corrects this. The following will detect a string field containing consecutive digits with leading or trailing blanks such that to_number(trim(string1)) will not fail

    LENGTH(TRIM(TRANSLATE(translate(trim(string1),' ','X'), '0123456789', ' '))) is null
    

提交回复
热议问题