sql 求带分隔符字符串中,重复字符串的个数
1个回答
展开全部
Create function Fn_ContantsStrCount--创建函数
(
@CurrStr varchar(max),--现有字符串
@FindStr varchar(50),--需要查找的字符串
@Chars varchar(5) = null--字符串中包含的字符
)
returns int
as
begin
declare @ResultCount int = 0 --计数
declare @Temp varchar(max) = ''
if(isnull(@Chars,'')='')
set @Chars = ','
while(CHARINDEX(@Chars,@CurrStr) > 0)
begin
set @Temp = REPLACE(SUBSTRING(@CurrStr,1,CHARINDEX(@Chars,@CurrStr)),@Chars,'')
set @CurrStr = SUBSTRING(@CurrStr,CHARINDEX(@Chars,@CurrStr) + 1,LEN(@CurrStr))
if(@Temp=@FindStr)
set @ResultCount += 1
end
if(@CurrStr = @FindStr)
set @ResultCount += 1
return @ResultCount
end
go
select dbo.Fn_ContantsStrCount('aaa:bbb:ccc:ddd:aaa:ccc:ddd:aaa','aaa',null)
select dbo.Fn_ContantsStrCount('aaa:bbb:ccc:ddd:aaa:ccc:ddd:aaa','aaa',':')
你看看满足你的要求不,函数,是可以放在查询语句中执行的东东
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询