17

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

4

7 回答 7

32

文档链接

CASE
  WHEN LENGTH(RTRIM(TRANSLATE(test_str, '*', ' 0123456789'))) = 0 
  THEN 'All digits'
  ELSE 'No'
END
于 2012-05-07T21:58:01.043 回答
5

如果您的 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+$')
于 2016-10-21T04:12:31.433 回答
3

有很多方法。仅使用两个函数查看该解决方案:

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

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

于 2016-02-21T21:14:29.780 回答
1

使用 ASCII 函数获取字符值并比较它在 48 '0' 和 57 '9' 之间

ASCII 表

ASCII 函数 以整数形式返回参数最左边字符的 ASCII 码值。

于 2012-05-07T22:00:43.977 回答
0

xQbert 的答案并不完全正确。您实际需要的是 fromString 中的每个字符的 * (并且需要删除空格),并且 to 字符串的长度需要与原始字符串的长度相同。

所以它看起来像这样:

CASE
  WHEN LENGTH(RTRIM(TRANSLATE(test_str, '**********', '0123456789'))) = LENGTH(RTRIM(test_str)) 
  THEN 'All digits'
  ELSE 'No'
END
于 2015-12-03T07:19:57.797 回答
0

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

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

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

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

返回值后带有尾随但不前导空格的数字。IE; 前导空格被视为非数字,但尾随空格被忽略。同样,对于大型机/Cobol CHAR 字段很常见:

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

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

where ( length(ltrim(rtrim(translate(substr(char_field,1,length(ltrim(rtrim(char_field)))),'         ','0123456789')))) = 0)
于 2015-12-29T19:01:19.607 回答
0

我根据 xQbert 公开的想法制作了更容易出错的版本,添加了中间结果,一些示例和 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
于 2016-03-24T22:47:34.033 回答