最后在同事的帮助下,我找到了可行的解决方案。它有一些限制(不考虑符号,不考虑小数部分),但对于 ID,它工作得很好。
- 从字符串的开头和结尾修剪空格
- 从字符串中修剪前导零
- 测试最大允许长度
- 用零填充字符串到四个字符(将四个零添加到字符串的开头并从字符串中获取最后四个字符)
- 在允许的字符集上测试字符串中的每个位置
所以SRC_TABLE
不能转换成的记录DECIMAL(4)
可以通过select来获取:
select
Some_Id
from
SRC_TABLE
where
characters(trim(leading '0' from trim(both ' ' from Some_Id))) > 4
or substring(substring('0000' || trim(leading '0' from trim(both ' ' from Some_Id)) FROM characters('0000' || trim(leading '0' from trim(both ' ' from Some_Id))) - 3) FROM 1 FOR 1) NOT IN ('0','1','2','3','4','5','6','7','8','9')
or substring(substring('0000' || trim(leading '0' from trim(both ' ' from Some_Id)) FROM characters('0000' || trim(leading '0' from trim(both ' ' from Some_Id))) - 3) FROM 2 FOR 1) NOT IN ('0','1','2','3','4','5','6','7','8','9')
or substring(substring('0000' || trim(leading '0' from trim(both ' ' from Some_Id)) FROM characters('0000' || trim(leading '0' from trim(both ' ' from Some_Id))) - 3) FROM 3 FOR 1) NOT IN ('0','1','2','3','4','5','6','7','8','9')
or substring(substring('0000' || trim(leading '0' from trim(both ' ' from Some_Id)) FROM characters('0000' || trim(leading '0' from trim(both ' ' from Some_Id))) - 3) FROM 4 FOR 1) NOT IN ('0','1','2','3','4','5','6','7','8','9');
编辑:dnoeth 在他对Convert char to int TeraData Sql的回答中建议的方式更方便,它也适用于 TD 13.10:
-- TO_NUMBER returns NULL when failing
CAST(TO_NUMBER(UTENTE_CD) AS INTEGER)
-- check if there are only digits
CASE WHEN UTENTE_CD = '' -- all spaces
THEN NULL
WHEN LTRIM(UTENTE_CD, '0123456789') = '' -- only digits
THEN CAST(UTENTE_CD AS INTEGER)
ELSE NULL
END