How do I check if a string contains a number

Using a regular expression:

SELECT *
FROM test
WHERE REGEXP_LIKE(testcol, '[[:digit:]]');

Not using regular expressions:

SELECT *
FROM test
WHERE testcol LIKE '%0%'
    OR testcol LIKE '%1%'
    OR testcol LIKE '%2%'
    OR testcol LIKE '%3%'
    OR testcol LIKE '%4%'
    OR testcol LIKE '%5%'
    OR testcol LIKE '%6%'
    OR testcol LIKE '%7%'
    OR testcol LIKE '%8%'
    OR testcol LIKE '%9%'

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.

Tags:

Sql

Oracle