Wednesday, August 29, 2012

Number of occurrences of space in a given string

I had used regular expression for counting number of occurrences of a character or string in SQL Oracle Database, but didnt know how to derive the same in SQL Server. 

Google search gave me the answer i was looking for:

declare @string varchar(50)
select @string = 'number of occurrences of space in a given string'
SELECT LEN(@string) - LEN(REPLACE(@string, ' ', '')) AS occurrence_count


Not sure what copyright policy the website has from where i got this piece of code, so not going to get into that....


Hope this helps.

No comments:

Post a Comment