GO ALTER FUNCTION CharCount(@input NVARCHAR(MAX),@pat NVARCHAR(MAX),@ignoreCase INT NULL=1) RETURNS INT BEGIN DECLARE @ct INT=0; DECLARE @idx INT=1; WHILE CHARINDEX(@pat,@input,@idx)>0 BEGIN SELECT @idx=CHARINDEX(@pat COLLATE Chinese_PRC_CS_AS_WS,@input,@idx)+1,@ct=@ct+1; END RETURN @ct; END