if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[f_getcharcount]') and xtype in (N'FN', N'IF', N'TF'))
drop function [dbo].[f_getcharcount]
GO
--得到一个字符串在另一个字符串中出现的次数
create function f_getcharcount(
@str varchar(8000),
@chr varchar(20)
) returns int
as
begin
declare @re int,@i int
select @re=0,@i=charindex(@chr,@str)+1
while @i>1
select @re=@re+1
,@str=substring(@str,@i,8000)
,@i=charindex(@chr,@str)+1
return(@re)
end
go
--调用示例
select dbo.f_getcharcount('aadddbbbbad','ad')
32.得到一个字符串在另一个字符串中出现的次数
本文介绍了一个SQL函数f_getcharcount的创建与使用方法,该函数用于计算一个字符串在另一字符串中出现的次数。通过T-SQL代码示例,展示了如何在SQL Server中定义和调用此函数。


被折叠的 条评论
为什么被折叠?



