|
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 intasbegindeclare @findlen intdeclare @times intset @findlen=LEN(@indexstr)set @times=0while(charindex(@indexstr,@str))>0BEGINset @str=SUBSTRING(@str,CHARINDEX(@indexstr,@str)+@findlen,len(@str))set @times=@times+1end return @timesendselect dbo.fn_str_times('adfarghbaaf','a')as 出现次数 |