select ProductsID 
from NetTrade2_Products 
where patindex(',%'+rtrim(ProductsCategoriesID)+',%',','+@categoryId+',')>0

解决方案 »

  1.   

    charindex(','+convert(varchar,@categoryId)+',',','+ProductsCategoriesID+',')>0 --樓主放錯位置了變量和列
      

  2.   

    这样查不到啊select * from NetTrade2_Products where patindex(',%'+rtrim(ProductsCategoriesID)+',%',',5,10,')>0  什么也查不到 
      

  3.   

    select * from NetTrade2_Products where patindex(',%'+rtrim(ProductsCategoriesID)+',%',',10,')>0 
    union all
    select * from NetTrade2_Products where patindex(',%'+rtrim(ProductsCategoriesID)+',%',',5,')>0 
      

  4.   


    --先创建字符串分割函数:
    IF OBJECT_ID('DBO.fn_split') IS NOT NULL
    DROP FUNCTION fn_split
    GOcreate function fn_split 

    @inputstr varchar(8000), 
    @seprator varchar(10) 

    returns @temp table (id varchar(200)) 
    as 
    begin 
    declare @i int 
    set @inputstr = rtrim(ltrim(@inputstr)) 
    set @i = charindex(@seprator, @inputstr) 
    while @i >= 1 
    begin 
    insert @temp values(left(@inputstr, @i - 1)) 
    set @inputstr = substring(@inputstr, @i + 1, len(@inputstr) - @i) 
    set @i = charindex(@seprator, @inputstr) 
    end 
    if @inputstr <> '\' 
    insert @temp values(@inputstr) 
    return 
    end
    --然后:
    --测试数据
    declare @ta table([id] int,[ids] nvarchar(100)) 
    Insert @ta 
    select 1,N'11,12,13' union all 
    select 2,N'32,45' union all 
    select 3,N'123,55' union all 
    select 4,N'48,32,564' union all 
    select 5,N'789,231,55,54' Select distinct a.[id], a.[ids] from @ta a,
     (select id from DBO.fn_split('32,13',',')) b -- 查找含 32,13 的记录
    where charindex(','+  b.[id] +',' , ','+a.[ids]+',')>0 
    /*
    id ids
    1 11,12,13
    2 32,45
    4 48,32,564
    */