IF EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[FN_MetindeBelirlenenKarekteriSay]') AND type in (N'FN', N'IF', N'TF', N'FS', N'FT'))
DROP FUNCTION  [dbo].[FN_MetindeBelirlenenKarekteriSay]
go


create  FUNCTION FN_MetindeBelirlenenKarekteriSay(@girilenveri nVARCHAR(4000),@bulunacakstr nvarchar(100) )
RETURNS int--(4000) 
AS 
BEGIN
DECLARE @dongu INT
DECLARE @str nVARCHAR(4000) ,
    @cikis nvARCHAR(4000)
SELECT  @str = @girilenveri
       SET @cikis = ''
SET @dongu = 1
WHILE @dongu < LEN(@str)
    BEGIN
        SET @cikis = @cikis
            + CASE WHEN SUBSTRING(@str, @dongu, 1)=@bulunacakstr 
                   THEN SUBSTRING(@str, @dongu, 1)
                   ELSE ''
              END
        SET @dongu = @dongu + 1
    END
RETURN  len(@cikis)

END


--SELECT dbo.OzelKarakterlerideCikaranFonksiyon('ab\23\e2\,4f$e','\')