id  
001
001001
001002
001003
001004
001003001
001004001
001003001001
输入参数是:001003001001
怎么样能得到
001
001003
001003001
001003001001

解决方案 »

  1.   

    select * from 你的表
    where '001003001001' like id+'%'
      

  2.   

    select id from tb where charindex(id,'001003001001')>0
      

  3.   

    declare @tb table(id  varchar(50))
    insert @tb
      select '001' union
      select '001001' union
      select '001002' union
      select '001003' union
      select '001004' union
      select '001003001' union
      select '001004001' union
      select '001003001001'declare @id varchar(50)
    set @id='001003001001'declare @tb1 table (id  varchar(50))
    declare @id_parent varchar(50)set @id_parent=@id
    while(len(@id_parent)>0)
    begin
      insert @tb1 select @id_parent
      set @id_parent=left(@id_parent,len(@id_parent)-3) 
    end 
    select * from @tb1 
      order by id
     /*
    001
    001003
    001003001
    001003001001
    */
      

  4.   

    select id from tb where patindex(id+'%','001003001001')>0
      

  5.   

    declare @tb table(id  varchar(50))
    insert @tb
      select '001' union
      select '001001' union
      select '001002' union
      select '001003' union
      select '001004' union
      select '001003001' union
      select '001004001' union
      select '001003001001'declare @id varchar(50)
    set @id='001003001001'
    select * from @tb
      where @id like id+'%'
      

  6.   

    declare @tb table(id  varchar(50))
    insert @tb
      select '001' union
      select '001001' union
      select '001002' union
      select '001003' union
      select '001004' union
      select '001003001' union
      select '001004001' union
      select '001003001001'declare @id varchar(50)
    set @id='001003001001'select * from @tb
      where @id like id+'%'
      

  7.   

    select * from 表 where '001003001001' like id+'%'
      

  8.   

    select * from 你的表
    where charindex(id,'001003001001')=1