当前位置: 代码迷 >> Sql Server >> SQL中计数的有关问题
  详细解决方案

SQL中计数的有关问题

热度:64   发布时间:2016-04-27 19:47:13.0
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 = ', '

/*
[email protected]@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
  相关解决方案