表名:测试Column_Name: ID列ID的格式正确- '^\d{4}-\d{6}-\d{3}-\d1}$‘
条件:必须与上述模式匹配,但不能以15开头,2-8,00,000,0000之间的数字。
正在使用REGEXP_LIKE匹配指定的条件,但无法在单个REGEXP_LIKE中包含start with方案:
with test as (
select '0614-210297-103-6' ID from dual union all
select '0014-210297-103-6' ID from dual union all
select '0004-210297-103-6' ID from dual union all
select '0000-210297-103-6' ID from dual union all
select '00120792-2..' ID from dual union all
select '0614- 210297-103-6' ID from dual union all
select '0614210297-103-6' ID from dual union all
select '2614-210297-103-6' ID from dual
)
select
case
when regexp_like(ID, '^\d{4}-\d{6}-\d{3}-\d{1}$')
then ID
else
case
when regexp_count(ID, '\d') = 14
then
case
when
not regexp_like(ID,'^15|^2-8|^00|^000|^0000')
then ID
end
else ID
end
end ID_tr
from test
发布于 2019-07-08 15:18:15
这可能是一种简化条件的方法,即使不是在单个regexp中:
regexp_like(ID, '^[190]\d{3}-\d{6}-\d{3}-\d$') and
substr(ID, 1, 2) not in ('00', '15')
在这里,我使用^[190]\d{3}
而不是^\d{4}
来仅在第一个数字不是2-8的情况下进行匹配;我发现避免以15或00,000,0000开头的字符串的唯一方法是使用substr
检查前两个字符。
有了你的数据,这个
select ID,
case
when regexp_like(ID, '^[190]\d{3}-\d{6}-\d{3}-\d$') and
substr(ID, 1, 2) not in ('00', '15') then ID
end as result
from test
提供:
ID RESULT
------------------ ------------------
0614-210297-103-6 0614-210297-103-6
0014-210297-103-6
0004-210297-103-6
0000-210297-103-6
00120792-2..
0614- 210297-103-6
0614210297-103-6
2614-210297-103-6
发布于 2019-07-08 15:52:06
你说“不能以15开头,2-8,00,000,0000”。
您可以将其简化为:不能以00、15、2n到8n开头。
或者你可以是正的而不是负的:必须从01到09,或者从10到14,或者从16到19或9n。
有了正条件,你可以得到一个REGEXP:
with test(id) as (
select '0614-210297-103-6' ID from dual union all
select '0014-210297-103-6' ID from dual union all
select '0004-210297-103-6' ID from dual union all
select '0000-210297-103-6' ID from dual union all
select '00120792-2..' ID from dual union all
select '0614- 210297-103-6' ID from dual union all
select '0614210297-103-6' ID from dual union all
select '1514-210297-103-6' ID from dual union all
select '1614-210297-103-6' ID from dual union all
select '2614-210297-103-6' ID from dual union all
select '9614-210297-103-6' ID from dual
)
select id from test
where regexp_like(id,'^(0[1-9]|1[1-46-9]|9\d)\d{2}-\d{6}-\d{3}-\d$')
ID
0614-210297-103-6
1614-210297-103-6
9614-210297-103-6
https://stackoverflow.com/questions/56929847
复制相似问题