Sql server query to count words and character

In this article I am going to explain how to count number of words and character using sql server.


 Implementation:
Sql server query to count words:

DECLARE @words VARCHAR(250)
SET @words = 'asp.net, sql server'

SELECT LEN(@words) - LEN(REPLACE(@words, ' ', '')) + 1 as 'number of words'

Result:
S.No.
number of words
1
3


Sql server query to count number of occurrence:
E.g. I want to count the occurrence of word ‘e’.

Declare @character varchar(100)
Set @character = 'sql server, asp.net'

Select Len(@character) - Len(Replace(@character, 'e', '')) as 'number of occurrence'

Result:
S.No.
Number of  occurrence
1
3




Post a Comment

0 Comments