SQL中计数的问题。
DECLARE @STR VACHAR(20)
SET @STR= '123,124,125,126,127 '
怎么处理
才能得到5呢,。
------解决方案--------------------select len(@str)-len(replace(@str, ', ', ' '))+1
------解决方案--------------------DECLARE @STR VArCHAR(20)
SET @STR= '123,124,125,126,127 '
Select DataLength(@STR)-DataLength(replace(@STR, ', ', ' '))+1 as t
------解决方案--------------------declare @Sub varchar(8000), @Str varchar(8000)
set @Str = '123,124,125,126,127 '
set @Sub = ', '
/*
返回@Sub在@Str中出现的次数。
*/
DECLARE @Start smallint, @Count smallint, @Index smallint, @Len smallint
SELECT @Count = 0, @Index = charindex(@Sub, @Str)
IF @Index > 0 SET @Len = len(@Sub)
WHILE @Index > 0
BEGIN
SET @Start = @Index + @Len
SELECT @Count = @Count + 1, @Index = charindex(@Sub, @Str, @Start)
END
select @Count,@Count+1