DB2- How to check if varchar field value has integers

后端 未结 7 892
借酒劲吻你
借酒劲吻你 2020-12-10 02:58

Is there an inbuilt DB2 function or any query to check if the character i have is a number? (I cannot use user defined functions)

相关标签:
7条回答
  • 2020-12-10 03:30

    Returns numeric where char field is all numerics with no leading or trailing spaces. ie; All characters in the field are numeric:

    where translate(char_field, 'X         ',' 0123456789') = ' '
    

    Returns non-numeric values with leading spaces considered non-numeric, but trailing spaces ignored. ie; non-numeric if there are leading spaces, but not if there are trailing spaces. This is a common occurrence for mainframe/Cobol-loaded fields:

    where not ( length(rtrim(translate(substr(char_field,1,length(rtrim(char_field))),'         ','0123456789'))) = 0)
    

    Returns numeric with trailing, but not leading spaces after value. ie; Leading spaces are treated as non-numeric, but trailing spaces are ignored. Again, common for mainframe/Cobol CHAR fields:

    where ( length(rtrim(translate(substr(char_field,1,length(rtrim(char_field))),'X         ',' 0123456789'))) = 0)
    

    Returns numeric with leading & trailing spaces. ie; ignores leading and trailing spaces in determining field is "numeric":

    where ( length(ltrim(rtrim(translate(substr(char_field,1,length(ltrim(rtrim(char_field)))),'         ','0123456789')))) = 0)
    
    0 讨论(0)
  • 2020-12-10 03:32

    if you version of db2 can use regexp_like you can do it:

    number with "." as decimal symbol:

     select *      from yourtable                            
     where REGEXP_LIKE(trim(yourzone) , '^\d+(\.\d*)?$')
    

    number with "," as decimal symbol:

     select *      from yourtable                            
     where REGEXP_LIKE(trim(yourzone) , '^\d+(\,\d*)?$') 
    

    number without decimal symbol ( integer only, your ask)

     select *      from yourtable                  
     where REGEXP_LIKE(trim(yourzone) , '^\d+$')
    
    0 讨论(0)
  • 2020-12-10 03:33

    There are many approaches. Take a look at that solution using only two functions:

    CASE
        WHEN REPLACE(TRANSLATE(test_str, '0','123456789','0'),'0','') = ''
        THEN 'All digits'
        ELSE 'Not all digits'
    END
    

    In general - less functions - better performance :)

    0 讨论(0)
  • 2020-12-10 03:46

    I have made more error-prone version based on the idea xQbert exposed, added intermedia result, some examples and to_integer column which converts string value safely to integer:

    select 
          test_str
        ,                  TRIM(TRANSLATE(replace(trim(test_str), ' ', 'x'), '           ', '0123456789'))
        , case when length(TRIM(TRANSLATE(replace(trim(test_str), ' ', 'x'), '           ', '0123456789')))=0 
          then cast(test_str as int) else null end to_integer
        , case when length(TRIM(TRANSLATE(replace(trim(test_str), ' ', 'x'), '           ', '0123456789')))=0 
          then 'integer'  else 'not integer' end is_integer
    from (VALUES 
            ('  123  '  )
            ,('  abc  ' )
            ,('  a12  ' )
            ,('  12 3  ')
            ,('  99.3  ')
            ,('993'     )
        ) AS X(test_str)
    ;
    

    The result for this example set is:

    TEST_STR 2        TO_INTEGER  IS_INTEGER
    -------- -------- ----------- -----------
      123                     123 integer
      abc    abc                - not integer
      a12    a                  - not integer
      12 3   x                  - not integer
      99.3   .                  - not integer
      993                     993 integer
    
    0 讨论(0)
  • 2020-12-10 03:48

    Use ASCII function to get character value and compare that it is between 48 '0' and 57 '9'

    ASCII Table

    ASCII Function Returns the ASCII code value of the leftmost character of the argument as an integer.

    0 讨论(0)
  • 2020-12-10 03:48

    The answer by xQbert is not completely correct. What you actually need is a * for every character in the fromString (and the space needs to be removed) and the length of the to string needs to be the same as the length of the original string.

    so it will look like this:

    CASE
      WHEN LENGTH(RTRIM(TRANSLATE(test_str, '**********', '0123456789'))) = LENGTH(RTRIM(test_str)) 
      THEN 'All digits'
      ELSE 'No'
    END
    
    0 讨论(0)
提交回复
热议问题