[英]find invalid characters in string
我需要一条选择语句,该语句将在“客户编号”字段中显示所有无效字符。
有效的客户编号以大写字母N开头,然后是10位数字,可以是0到9。
就像是,
SELECT (CustomerField, 'N[0-9](10)') <> ''
FROM CustomerTable;
使用regexp_like
。
select customerfield
from CustomerTable
where not regexp_like(CustomerField, '^N[0-9]{10}$')
这将显示不遵循指定模式的customerfield。
如果您确实需要在字符串中查找无效字符(而不仅仅是查找无效的字符串),那么这种更为复杂的查询可能会有所帮助。 您没有说明可能需要哪种格式的输出,因此我自己编写了格式。 我还创建了几个测试字符串(尤其是,检查NULL
输入是否正确对待总是很重要的)。
len
列显示输入的长度(如果不是11)。空字符串的长度(在Oracle中为null
)显示为0。第first-nondigit
列表示从字符串的SECOND位置开始的字符(忽略规则不同的第一个字符,并分别检查其有效性)。
with
inputs ( str ) as (
select 'N0123456789' from dual union all
select '' from dual union all
select '02324434323' from dual union all
select 'N02345678' from dual union all
select 'A2140480080' from dual union all
select 'N93049c4995' from dual union all
select 'N4448883333' from dual union all
select 'PAR3993949Z' from dual union all
select 'AN39E' from dual
)
-- end of test data; query begins below this line
select str,
case when regexp_like(str, '^N\d{10}$') then 'valid'
else 'invalid' end as classif,
case when length(str) != 11 then length(str)
when str is null then 0 end as len,
case when substr(str, 1, 1) != 'N'
then substr(str, 1, 1) end as first_char,
regexp_substr(str, '[^0-9]', 2) as first_nondigit,
nullif(regexp_instr( str, '[^0-9]', 2), 0) as first_nondigit_pos
from inputs
;
输出值
STR CLASSIF LEN FIRST_CHAR FIRST_NONDIG FIRST_NONDIGIT_POS
----------- ------- ----- ---------- ------------ ------------------
N0123456789 valid
invalid 0
02324434323 invalid 0
N02345678 invalid 9
A2140480080 invalid A
N93049c4995 invalid c 7
N4448883333 valid
PAR3993949Z invalid P A 2
AN39E invalid 5 A N 2
9 rows selected.
\\ d代表数字
其余的如果可以在这里找到正则表达式元素
select *
from CustomerTable
where not regexp_like (CustomerField,'^N\d{10}$')
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.