SQL 根据规律查找数据
用户设置了一个顺序 234
2必须为数据的开头。
如果数据片段符合下面几种情况,则返回true.
2,1,2,3,1,2,4 (234之间隔2位)
2,1,2,3,3,1,2,3,4 (234之间隔3位)
2,1,2,3,4,3,1,2,3,4,4 (234之间隔4位)
2,1,2,3,4,5,3,1,2,3,4,5,4 (234之间隔5位)
那么 如果数据为
2,1,1,1,1,1,3,1,1,1,1,1,4,1,1,1,1,1,1,1,
怎么写SQL,判断234 是否在上面4种情况
[最优解释]
declare @str varchar(20)='2,1,2,3,1,2,4'
declare @return int=0
select @return=1
from (select @str as f ) as a
where f like '%2_____3_____4%'
or f like '%2_______3_______4%'
or f like '%2_________3_________4%'
or f like '%2___________3___________4%'
select @return
select * from a where a like'%2%3%4%'
select col
from tb,master..spt_values b
where
b.type='P'
and LEFT(col,1)='2'
and SUBSTRING(tb,1+number,1)='3'
and SUBSTRING(tb,1+2*number,1)='4'
select col
from tb,master..spt_values b
where
b.type='P'
and LEFT(col,1)='2'
and SUBSTRING(col,1+number,1)='3'
and SUBSTRING(col,1+2*number,1)='4'