翻过高山走不出你
1:利用len函数
declare @a varchar(20)
set @a='adfarghbaaf'
select len(@a)- len(replace(@a,'a',''))
2:自定义一个函数
create function fn_str_times
(
@str varchar(1000),--原子符串
@indexstr varchar(20)--查找的字符
)
returns int
as
begin
declare @findlen int
declare @times int
set @findlen=LEN(@indexstr)
set @times=0
while(charindex(@indexstr,@str))>0
BEGIN
set @str=SUBSTRING(@str,CHARINDEX(@indexstr,@str)+@findlen,len(@str))
set @times=@times+1
end
return @times
end
select dbo.fn_str_times('adfarghbaaf','a')as 出现次数