我们在临时表中加载的供应商数据相当脏。一列特别捕获数字数据,但40%的时间具有垃圾字符或随机字符串。
我必须创建一个过滤掉该列中值范围的报告。所以,我尝试使用替换/翻译这样的组合
select replace(translate(upper(str),' ','all possible char'),' ','')
from table
但是每当遇到我没有编码的字符时它就会失败。因此,报告永远不会自动化。
Javascript具有isNaN()函数来确定某个值是否为非法数字(如果是,则为True,否则为false)。
我如何用DB2做同样的事情?你有什么想法吗?
提前致谢。
答案 0 :(得分:10)
一种相当可靠(但稍微有些hackish)的方法是将字符串与其大小写自我进行比较(数字没有不同的情况)。只要带入字符的数据只包含拉丁字符,您应该没问题:
SELECT input, CASE
WHEN UPPER(input) = LOWER(input) THEN TO_NUMBER(input)
ELSE 0
END AS output
FROM source
另一个选择是使用TRANSLATE
功能:
SELECT input,
CASE
WHEN TRANSLATE(CAST(input as CHAR(10)), '~~~~~~~~~~~~~', '0123456789-. ') = '~~~~~~~~~~' THEN CAST(input AS DECIMAL(12, 2))
ELSE 0
END AS num
FROM x
答案 1 :(得分:2)
WITH x (stringval) AS
(
VALUES ('x2'),(''),('2.2.'),('5-'),('-5-'),('--5'),('.5'),('2 2'),('0.5-'),(' 1 '),('2 '),('3.'),('-4.0')
)
SELECT stringval,
CASE WHEN (
-- Whitespace must not appear in the middle of a number
-- (but trailing and/or leading whitespace is permitted)
RTRIM(LTRIM( stringval )) NOT LIKE '% %'
-- A number cannot start with a decimal point
AND LTRIM( stringval ) NOT LIKE '.%'
-- A negative decimal number must contain at least one digit between
-- the negative sign and the decimal point
AND LTRIM( stringval ) NOT LIKE '-.%'
-- The negative sign may only appear at the beginning of the number
AND LOCATE( '-', LTRIM(stringval)) IN ( 0, 1 )
-- A number must contain at least one digit
AND TRANSLATE( stringval, '0000000000', '123456789') LIKE '%0%'
-- Allow up to one negative sign, followed by up to one decimal point
AND REPLACE(
TRANSLATE( RTRIM(LTRIM(stringval)), '000000000', '123456789'),
'0', '') IN ('','-','.','-.')
)
THEN 'VALID'
ELSE 'INVALID'
END AS stringisvalidnumber
FROM x
;
答案 2 :(得分:1)
检查出来:
SELECT Mobile,
TRANSLATE(Mobile, '~~~~~~~~~~', '0123456789') AS FirstPass,
TRANSLATE(TRANSLATE(Mobile, '~~~~~~~~~~', '0123456789'), '', '~') AS Erroneous,
REPLACE(TRANSLATE(Mobile, '', TRANSLATE(TRANSLATE(Mobile, '~~~~~~~~~~', '0123456789'), '', '~')), ' ', '') AS Corrected
FROM Person WHERE Mobile <> '' FETCH FIRST 100 ROWS ONLY
表格为“人物”,您要检查的字段为“移动”。 如果你对此有所了解,可以构建一个UPDATE来修复整个表