DB2-如何检查varchar字段值是否为整数

70gysomp  于 2022-11-07  发布在  DB2
关注(0)|答案(7)|浏览(586)

是否有一个内置的DB2函数或任何查询来检查我所拥有的字符是否是数字?(我不能使用用户定义的函数)

yfwxisqw

yfwxisqw1#

Doc Link

CASE
  WHEN LENGTH(RTRIM(TRANSLATE(test_str, '*', ' 0123456789'))) = 0 
  THEN 'All digits'
  ELSE 'No'
END
ndh0cuux

ndh0cuux2#

如果您版本db2可以使用regexp_like,那么您可以这样做:
以“.”作为十进制符号的数字:

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

带“”的数字,作为十进制符号:

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

不带小数点的数字(仅限整数,您的要价)

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

vmdwslir3#

有很多方法,看看这个只使用两个函数的解决方案:

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

总的来说-功能更少-性能更好:)

8cdiaqws

8cdiaqws4#

使用ASCII函数获取字符值,并比较它是否在48 '0'和57 '9'之间
ASCII Table
ASCII函数以整数形式返回参数最左边字符的ASCII码值。

webghufk

webghufk5#

xQbert给出的答案并不完全正确,实际上需要的是fromString中的每个字符都有一个 *(并且需要去掉空格),并且to字符串的长度需要与原始字符串的长度相同。
因此它看起来像这样:

CASE
  WHEN LENGTH(RTRIM(TRANSLATE(test_str, '**********', '0123456789'))) = LENGTH(RTRIM(test_str)) 
  THEN 'All digits'
  ELSE 'No'
END
toiithl6

toiithl66#

返回数值,其中char字段是不带前导空格或尾随空格的所有数值。即;字段中的所有字符都是数字:

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

返回前导空格被视为非数字但尾部空格被忽略的非数字值。如果有前导空格,则为非数字,但如果有尾随空格,则不为非数字。这是大型机/Cobol加载的字段的常见情况:

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

返回值后带有尾随空格但不带有前导空格的数值。即;前导空格将被视为非数字,但尾部空格将被忽略。同样,大型机/Cobol CHAR字段的常见情况如下:

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

返回带有前导空格和尾随空格的数字。即;在确定字段为“numeric”时忽略前导空格和尾随空格:

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

lyr7nygr7#

基于xQbert公开的思想,我做了一个更容易出错的版本,添加了intermedia result,一些例子和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)
;

此示例集的结果为:

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

相关问题