正则表达式仅在SQL中验证前两个字符

时间:2013-12-27 11:19:41

标签: sql-server regex

我要求检查一个值是否仅包含前两个字符中的字母D,F,I,Q,U或V

在SQL中,我尝试过

LIKE '[DFIQUV][DFIQUV]%'

没有运气。它不验证第二个字符是否是所列字符之一(适用于第一个字符)

我做错了什么?

2 个答案:

答案 0 :(得分:1)

分开检查前两个字符的每一个:

WHERE (COL LIKE '[DFIQUV]%' OR COL LIKE '_[DFIQUV]%')

答案 1 :(得分:0)

如果我理解正确,你需要这样的东西:

create table #a(col varchar(10))

insert #a(col)
select 'ABC' union all
select 'EFG' union all
select 'DFI' union all
select 'QUV'

--unnecessary rows
select * from #a where col not like '[DFIQUV][DFIQUV]%' --1st version (means 'and' condition between two letters)
select * from #a where col not like '[DFIQUV]%' or col like '_[DFIQUV]%' --correct version (manually 'or' condition)

--necessary rows
select * from #a where col not like '[DFIQUV]%' and col not like '_[DFIQUV]%'