create FUNCTION [dbo].[fun_RepeatCharNum]
(
@CodeString varchar(2000),
@RepeatString varchar(200)
)
RETURNS int
AS
BEGIN
DECLARE
@RepeatStringLen int, --检测重复字符的长串
@TempString varchar(500) --临时字符串(总比@RepeatString多一个即长度大1)
SET @RepeatStringLen=LEN(@RepeatString)
SET @TempString=Replicate('|',@RepeatStringLen)+'|'
RETURN LEN(REPLACE(@CodeString,@RepeatString,@TempString))-LEN(@CodeString)
END
在Sql2005下测试如下
SELECT dbo.fun_RepeatCharNum('adfasdfas||s|||dfas','|')