declare @str varchar(100)
set @str='1,2,4,23,54'
if charindex(',2,', replace(','+@str+',', ' ', ''))<>0
print 'match'
else print 'not match'

解决方案 »

  1.   

    select * from [table] where [ziduan] like '%2%'
      

  2.   

    顺便说一下,楼主这个问题是很common的问题。
    CSDN上最近出现了N次了。
    楼上的方法是不安全的。建议楼主以后提问时,先搜索一下。
    如果前人已经问过了的话,你就不需要浪费分数来提问了。
    而且,获得知识能力也是很重要的。下面是我QQ的签名:
    百度可以搜到的问题, 不要问.
    调试可以出来的问题, 不要问.
    没有确切答案的问题, 不要问.真心提醒。不要见怪。
      

  3.   

    --楼主,我实现的方法很复杂,
    --原理如下:将字符串以","分为表的一个字段,然后在选
    --尽供参考
    declare  @t  table(id  int,point  varchar(100))  
    insert  @t  
    select  1,        '1,2,4,23,54' 
    declare  @ret  table(id  int,point  varchar(100))--返回表  
    declare  @id  int,@point  varchar(100)  
    declare  c  cursor  for  
    select  id,point  from  @t  
    open  c  
    fetch  next  from  c  into  @id,@point  
    while  @@fetch_status  =  0  
    begin  
       WHILE  CHARINDEX(',',@point)>0  
       BEGIN  
           INSERT  INTO  @ret(id,point)  VALUES(@id,LEFT(@point,CHARINDEX(',',@point)-1))  
           SET  @point=RIGHT(@point,LEN(@point)-CHARINDEX(',',@point))  
       END  
       INSERT  INTO  @ret(id,point)  VALUES(@id,@point)  
       fetch  next  from  c  into  @id,@point  
    end  
    close  c  
    deallocate  c  
     
    select  *  from  @ret where point like '%2%'
      

  4.   

    select * from [table] where CAST([ziduan] AS NVARCHAR(50)) like '%2%'
      

  5.   

    支持aw511,但做如下修改后会更好
    declare  @t  table(id  int,point  varchar(100))  
    insert  @t  
    select  1,        '1,2,4,23,54' 
    declare  @ret  table(id  int,point  varchar(100))--返回表  
    declare  @id  int,@point  varchar(100) 
    set @id =1 
    declare  c  cursor  for  
    select  id,point  from  @t  
    open  c  
    fetch  next  from  c  into  @id,@point  
    while  @@fetch_status  =  0  
    begin  
       WHILE  CHARINDEX(',',@point)>0  
       BEGIN         INSERT  INTO  @ret(id,point)  VALUES(@id,LEFT(@point,CHARINDEX(',',@point)-1))  
           SET  @point=RIGHT(@point,LEN(@point)-CHARINDEX(',',@point))  
          set  @id =@id +1--------使id 加1   END  
     set  @id =@id +1   --------使id 加1
       INSERT  INTO  @ret(id,point)  VALUES(@id,@point)  
       fetch  next  from  c  into  @id,@point  
    end  
    close  c  
    deallocate  c  
     
    select  *  from  @ret where point like '%2%'
      

  6.   

    select * from 表 where charindex(',2,',','+字段名+',')>0orselect * from 表 where ','+字段+',' like '%,2,%'