Write a script to identify, Each character's count in a given string ? (Without using Loop)
i.e: Pandian

 Posted by PandianS on 2/9/2012 | Category: Sql Server Interview questions | Views: 2275 | Points: 40
Answer:

Declare @String Varchar(100)

Select @String = 'Pandian'
;With CTEs
As
(
Select LEFT(@String,0) Chars,0 [String]
Union All
Select Substring(@String,[String]+1,1) Chars,[String]+1 From CTEs Where [String] <=LEN(@String)
)
Select Chars [Letter], COUNT(1) [Repeats] from CTEs Where Chars <>'' Group by Chars
Go
Result:
Letter	Repeats

------ ------
a 2
d 1
i 1
n 2
P 1


Asked In: Many Interviews | Alert Moderator 

Comments or Responses

Posted by: Chvrsri on: 2/10/2012 | Points: 10
Great post !! Very useful

Login to post response