我想在字符串如:2,3,5,11,26 中查找
26的位置索引。
用charindex得到的结果是10
我想要得到的索引为5
请赐教

解决方案 »

  1.   

    select len('2,3,5,11,26')-len(replace('2,3,5,11,26',',',''))+1
    --------------------
    这样也可以实现,查出逗号的个数加1即为26的位置
      

  2.   

    select   (len('2,3,5,11,26')-len(replace('2,3,5,11,26',',','')))+1 
      

  3.   


    create proc f_search
    @keyword int,
    @text nvarchar(2000),
    @ret  int out
    as
    begin
    create table ##(id int identity(1,1),skey int)
    declare @s varchar(8000)
        set @s = 'insert ##(skey) select '+replace(@text,',',' union select ')
    exec(@s)
        select @ret = id from ## where skey  = @keyword
        drop table ##
    end
    go
    declare @i int
    exec f_search 26,'2,3,5,11,26',@I out
    select @I
    drop proc f_search
    /*----------- 
    5(所影响的行数为 1 行)*/
      

  4.   

    declare @s varchar(100),@count int
    set @s='2,3,5,11,26'
    set @count=0
    while(charindex(',',@s)>0)
    begin
       set @s=substring(@s,(charindex(',','2,3,5,11,26'))+1,len(@s))
       set @count=@count+1
    end
    select @count
    --
    很简单,接分了