如何在sql server中判断某字段中重复出现某字符多次

如题,假设某字段的其中 一个值是‘Fire safety;Fire sprinklers;Inspection;Installation;Maintenance’;我想知道其中出现了几次‘;’,以及每次‘;’所在的位置 谢谢各位 我 用的是sql 2005

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 出现次数
温馨提示:答案为网友推荐,仅供参考
第1个回答  2011-08-23
重复字段和重复次数:
select 字段名,count(字段名) from 表名 group by 字段名 having count(字段名)>1
第2个回答  推荐于2016-02-04
DECLARE @s NVARCHAR(200)
SET @s='Fire safety;Fire sprinklers;Inspection;Installation;Maintenance'

SELECT
number
FROM master.dbo.spt_values AS a
WHERE number>0 AND type='P' AND SUBSTRING(@s,number,1)=';'

/*
12
28
39
52
*/本回答被提问者采纳