分拆合并列值...
参考这个 http://blog.csdn.net/usher_gml/archive/2009/04/27/4128092.aspx

解决方案 »

  1.   

    declare @s varchar(100),@sql varchar(1000)
    set @s = '20,50'
    set @sql = replace(@s,',',''' as id union all select ''')
    set @sql = 'select '''+@sql + ''''select @sql = 'select * from ta a,('+@sql+') b where a.SSItemID = b.id and charindex('',''+id+'','','',''+SSItemID+'','') >0'
    exec( @sql)
      

  2.   

    首先得把要比较的值拆开再比较,不然    charindex('20,11',',1,20,33,11,23,')  也照样是 0
      

  3.   

    --> Test Data: [tabel1]
    if object_id('[tabel1]') is not null drop table [tabel1]
    create table [tabel1] ([SSItemID] varchar(50))
    insert into [tabel1]
    select '1,20,33,11,23'
    go
    --select * from [tabel1]
    --Code
    declare @s varchar(50)
    declare @sql varchar(2000)
    begin
    create table #tmp(num varchar(10))
    set @s='20,55'
    select @sql='select '+replace(@s,',',' union all select ')
    insert #tmp exec(@sql) 
    exec(@sql) 
    select distinct a.* from [tabel1] a,#tmp b where charindex(','+b.num+',',','+a.SSItemID+',')>0drop table #tmp
    end
    --Drop
    drop table tabel1
    --Result
    /*
    SSItemID                                           
    -------------------------------------------------- 
    1,20,33,11,23
    */
      

  4.   

    你的数据是 1,20,33,11,23 

    select  SSItemID  from xx  where  charindex('20,55',','+rtrim(SSItemID)+',') >0  怎么能查出来?
    select  SSItemID  from xx  where  charindex('20,33',','+rtrim(SSItemID)+',') >0  应该。。