Click here to monitor SSC
SQLServerCentral is supported by Red Gate Software Ltd.
 
Log in  ::  Register  ::  Not logged in
 
 
 
        
Home       Members    Calendar    Who's On


Add to briefcase

Counting ocurrences in a string Expand / Collapse
Author
Message
Posted Monday, October 1, 2007 9:38 AM
Forum Newbie

Forum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum Newbie

Group: General Forum Members
Last Login: Monday, July 11, 2005 2:33 PM
Points: 5, Visits: 1
Comments posted to this topic are about the item Counting ocurrences in a string


Post #405097
Posted Saturday, October 2, 2010 10:25 AM
Grasshopper

GrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopper

Group: General Forum Members
Last Login: Saturday, October 11, 2014 5:17 AM
Points: 14, Visits: 259
I modified your code to make it more useful.

DECLARE @s NVARCHAR(MAX)  
DECLARE @result INT
DECLARE @count_me NVARCHAR(MAX)

/*
The string to be searched.
*/
SET @s = '1101001001110101'

/*
The character to be counted.
*/
SET @count_me = '00'

/*
Replace the character to be counted with empty string,
and subtract the lengths.
*/
SET @result = (LEN(@s) - LEN(REPLACE(@s,@count_me,'')) )/LEN(@count_me)

/*
Do something with the result
*/
PRINT @result

Post #997229
« Prev Topic | Next Topic »

Add to briefcase

Permissions Expand / Collapse