SQL Server function for Count the Occurrence of Character in the string
============================================================
-- Author : Milind Kansagara
-- Date : 30 July, 2007
-- Desc : Count the Occurrence of Character in the string
-- Usage : select dbo.GetOccurrence('Abhishek Joshi','a')
CREATE FUNCTION [dbo].[GetOccurrence] ( @pInput VARCHAR(1000), @pSearchChar CHAR(1) )
RETURNS INT
BEGIN
DECLARE @vInputLength INT
DECLARE @vIndex INT
DECLARE @vCount INT
SET @vCount = 0
SET @vIndex = 1
SET @vInputLength = LEN(@pInput)
WHILE @vIndex <= @vInputLength
BEGIN
IF SUBSTRING(@pInput, @vIndex, 1) = @pSearchChar
SET @vCount = @vCount + 1
SET @vIndex = @vIndex + 1
END
RETURN @vCount
END
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment