check if “it's a number” function in Oracle

后端 未结 16 2766
一整个雨季
一整个雨季 2020-11-28 10:31

I\'m trying to check if a value from a column in an oracle (10g) query is a number in order to compare it. Something like:

select case when ( is_number(myTab         


        
16条回答
  •  抹茶落季
    2020-11-28 11:11

    This is a potential duplicate of Finding rows that don't contain numeric data in Oracle. Also see: How can I determine if a string is numeric in SQL?.

    Here's a solution based on Michael Durrant's that works for integers.

    SELECT foo
    FROM bar
    WHERE DECODE(TRIM(TRANSLATE(your_number,'0123456789',' ')), NULL, 'number','contains char') = 'number'
    

    Adrian Carneiro posted a solution that works for decimals and others. However, as Justin Cave pointed out, this will incorrectly classify strings like '123.45.23.234' or '131+234'.

    SELECT foo
    FROM bar
    WHERE DECODE(TRIM(TRANSLATE(your_number,'+-.0123456789',' ')), NULL, 'number','contains char') = 'number'
    

    If you need a solution without PL/SQL or REGEXP_LIKE, this may help.

提交回复
热议问题