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.
I would try using regex. Like:
select regexp_instr('%[0-9]%',@str1) from table_name;
Cheers --Jocke
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.
I sometimes use REGEXP_INSTR in NetSuite saved searches since the SQL functions are limited. It returns a zero if no numbers are found, or the starting position of the first number found in the string:
REGEXP_INSTR(my_var, '[[:digit:]]')
This example returns zero:
REGEXP_INSTR('test', '[[:digit:]]')
This example returns a number greater than zero since it found a number:
REGEXP_INSTR('test2', '[[:digit:]]')