用户设置了一个顺序 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种情况
 

解决方案 »

  1.   

    select * from a where a like'%2%3%4%'
      

  2.   

    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
      

  3.   

    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'
      

  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'
      

  5.   


    非常感谢,如果用户要求 234之间能隔1-9位这9九情况,这样要写很多or f like 
    SQL 能不能优化 
      

  6.   

    where charindex(字段,'2')=1 
          and 2*charindex(字段,'3')-charindex(字段,'4')=1
      

  7.   

    间隔没关系:like '2%3%4%'
    有关系:like '2_3_4%'