How do I check if a string contains a number

前端 未结 4 1452
北海茫月
北海茫月 2021-01-12 10:34

I need to check if a string contains a number. Any number. Not wether or not the string IS a number, but if it contains one.

Examples:

\'test\' = no numbers.

4条回答
  •  没有蜡笔的小新
    2021-01-12 11:30

    This is the technique that I use to detect if a string contains a number:

    select LAST_NAME, 'contains a number'
      FROM names
     where translate(LAST_NAME, '0123456789', '') <> LAST_NAME
    

    That works by translating the digits to empty string. If the string remains the same, then it could not have contained digits.

    This technique also works to test if a string is all numbers

    select TEL_NUMBER, 'is all numbers'
      FROM names
     where trim(translate(TEL_NUMBER, '-0123456789', '')) = ''
    

    For the telephone number I included a dash because telephone numbers typically contain dashes.

提交回复
热议问题