TSQL program to find how many times a word repeated in a given string

Rajnilari2015
Posted by Rajnilari2015 under Sql Server category on | Points: 40 | Views : 195
DECLARE @T TABLE(Word VARCHAR(100))
INSERT INTO @T VALUES ('hello'),('this'),('is'),('dotnet'),('dotnet'),('funda'),('is'),('is')
SELECT
Word
,CountOfWords = IIF(COUNT(Word)=1,
'Word ' + Word + ' has never repeated',
'Word ' + Word + ' repeated ' + CAST(COUNT(Word) AS VARCHAR(100)) + ' times')

FROM @T
GROUP BY Word
ORDER BY 1


/*

Word	CountOfWords

dotnet Word dotnet repeated 2 times
funda Word funda has never repeated
hello Word hello has never repeated
is Word is repeated 3 times
this Word this has never repeated


*/

Comments or Responses

Login to post response