-- ============================================= -- Author: <何剑伟> -- Create date: <2008-03-19> -- Description: <获取一个长字符串中某个字符串出现次数> -- ============================================= ALTER 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 ' , ' | ' )