Microsoft SQL Server 2005 Best Practices

How to create a function to count characters in a string?

 

Execute the following script in Query Editor to create the counting function:

USE AdventureWorks;

create function dbo.fnCharCount (@Text varchar(8000), @Char char(1))
returns int
as
begin
declare @Occur int
select @Occur = len(@Text) -
len(replace(@Text,@Char,''))
return @occur
end

-- select dbo.fnCharCount('This, is a, sample,,, with, commas', ',')


 

The Best SQL Server 2005 Training in the World
 
 
SQLUSA.com Home Page