日期:2014-05-17 浏览次数:20428 次
create function AccRepeat(@str varchar(50),@sub varchar(50))
returns int
as
begin
declare @pos int,@n int
select @n=0, @pos=charindex(@sub,@str)
while(@pos<>0)
begin
select @str=right(@str,len(@str)-@pos),@pos=charindex(@sub,@str),@n=@n+1
end
return(@n)
end
go
WITH huang (test_ids) AS (SELECT '11,22,33,43,53')
SELECT dbo.AccRepeat(test_ids,',')+1 FROM huang
declare @val varchar(40),@ind int,@count int
set @val='11,22,33,43,53'
set @ind=CHARINDEX(',',@val)
set @count=1
while @ind>0
begin
set @val=RIGHT(@val,len(@val)-@ind)
set @ind=CHARINDEX( ',',@val)
set @count=@count+1
end
select @count