• Twitter
  • Facebook
  • Google+
  • Instagram
  • Youtube

Monday, July 30, 2007

SQL Server function for Count the Occurrence of Character in the string

SQL Server function for Count the Occurrence of Character in the string
============================================================
-- Author : Abhishek Joshi
-- 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

0 comments:

Contact

Get in touch with me


Adress/Street

12 Street West Victoria 1234 Australia

Phone number

+(12) 3456 789

Website

www.johnsmith.com