CREATE FUNCTION [dbo].[com_CountString](@Input nVarChar(max),
@SearchString nVarChar(1000))
RETURNS INT
BEGIN
if @Input is null or @SearchString is null
return 0
DECLARE @InputLength INT, @SearchLength INT
SELECT @InputLength = LEN(@Input), @SearchLength = LEN(@SearchString)
IF @InputLength = 0 or @SearchLength = 0 or @SearchLength > @InputLength
return 0
RETURN (LEN(@input) - LEN(REPLACE(@input, @SearchString, ''))) / LEN(@SearchString)
END