select top 100 id=identity(int,1,1) into # from syscolumns
declare @t table(col varchar(100))
insert @t select '张三,王五,李四'select * into #1 from
(
    select s=substring(a.col,b.id,charindex(',',a.col+',',b.id)-b.id)
    from @t a,# b
    where substring(','+a.col,b.id,1)=','
    group by substring(a.col,b.id,charindex(',',a.col+',',b.id)-b.id)
) aselect count(1) from #1drop table #,#1

解决方案 »

  1.   

    declare @string nvarchar(100),@count int,@sql varchar(8000)
    select @string= '张三,王五,李四',@sql=''select @count=len(@string)-len(replace(@string,',',''))+1  --统计数量方法一
    select @sql='select '''+replace(@string,',',''' union all select ''')+''''
    exec (@sql)--分割字符串
    select @count=@@rowcount  --统计数量方法二
      

  2.   

    declare @s as varchar(100)
    set @s = '张三,王五,李四'select len(@s) - len(replace(@s , ',' , '')) + 1/*-----------
    3(1 行受影响)
    */