解决方案 »

  1.   

    ----------------------------------------------------------------
    -- Author  :fredrickhu(小F,向高手学习)
    -- Date    :2014-04-09 16:02:18
    -- Verstion:
    --      Microsoft SQL Server 2008 (RTM) - 10.0.1600.22 (Intel X86) 
    -- Jul  9 2008 14:43:34 
    -- Copyright (c) 1988-2008 Microsoft Corporation
    -- Enterprise Edition on Windows NT 6.1 <X86> (Build 7601: Service Pack 1)
    --
    ----------------------------------------------------------------
    --> 测试数据:[tb]
    if object_id('[tb]') is not null drop table [tb]
    go 
    create table [tb]([cid] varchar(11))
    insert [tb]
    select '12;30;16;21'
    --------------开始查询--------------------------
    DECLARE @s VARCHAR(MAX)
    SET @s='10;12;18;30;50'
    SELECT *  FROM 
    (
    Select
        cid=substring(a.cid,b.number,charindex(';',a.cid+';',b.number)-b.number) 
    from 
        Tb a join master..spt_values  b 
        ON B.type='p' AND B.number BETWEEN 1 AND LEN(A.cid)
    where
         substring(';'+a.cid,b.number,1)=';')t
    WHERE 
       CHARINDEX(';'+cid+';',';'+@s+';')>0
    ----------------结果----------------------------
    /* cid
    -----------
    12
    30(2 行受影响)*/