How do I check if a string contains a number

前端 未结 4 1418
北海茫月
北海茫月 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:19

    I would try using regex. Like:

    select regexp_instr('%[0-9]%',@str1) from table_name;
    

    Cheers --Jocke

    0 讨论(0)
  • 2021-01-12 11:20

    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%'
    
    0 讨论(0)
  • 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.

    0 讨论(0)
  • 2021-01-12 11:34

    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:]]')
    
    0 讨论(0)
提交回复
热议问题