查询一个字符在一个字符串中出现的次数

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
select dbo.AccRepeat('MicrosoftSQLServer','s') 
------
3

  

猜你喜欢

转载自www.cnblogs.com/salv/p/11355722.html