Golgappa.net | Golgappa.org | BagIndia.net | BodyIndia.Com | CabIndia.net | CarsBikes.net | CarsBikes.org | CashIndia.net | ConsumerIndia.net | CookingIndia.net | DataIndia.net | DealIndia.net | EmailIndia.net | FirstTablet.com | FirstTourist.com | ForsaleIndia.net | IndiaBody.Com | IndiaCab.net | IndiaCash.net | IndiaModel.net | KidForum.net | OfficeIndia.net | PaysIndia.com | RestaurantIndia.net | RestaurantsIndia.net | SaleForum.net | SellForum.net | SoldIndia.com | StarIndia.net | TomatoCab.com | TomatoCabs.com | TownIndia.com
Interested to Buy Any Domain ? << Click Here >> for more details...

How can count the string ?
for ex: If i have string like 'bhaskar' then i need like
b:1
h:1
a:2
s:1
k:1
r:1

please give any idea on that


Answer Posted / santosh kairamkonda

In SQL Server 2005 New query window for any database.

declare @char as charchar(20)
set @char='bhaskar'

declare @i as int
declare @cnt as int

set @i=1
set @cnt=1

Create table #temp (charval varchar(4) )

while (@i < len(@char) )
begin
set @cnt = @cnt + (select count(1) from #temp where
substring(charval,1,1) = substring(@char,@i,1))

if (@cnt>1)
begin
update #temp set charval = (substring
(@char,@i,1)+':'+ Convert(varchar(20),@cnt) ) where
substring(charval,1,1) = substring(@char,@i,1)
end
else
begin
Insert into #temp values (substring
(@char,@i,1)+':'+ Convert(varchar(20),@cnt) )
end

print @i
set @i = @i+1

set @cnt=1
end

select * from #temp

=======================
You will get result as required.

Is This Answer Correct ?    2 Yes 2 No



Post New Answer       View All Answers


Please Help Members By Posting Answers For Below Questions

What is log in sql server?

1123


Difference between LEN() and DATALENGTH() in sql server ?

1087


What is 'Join' and explain its various types.

1112


How to read data in a table with "select" statements?

950


How to create new tables with "select ... Into" statements in ms sql server?

973


How to stop a loop early with break statements in ms sql server?

995


How many databases Microsoft SQL server provides?

1065


What do you mean by recursive stored procedure?

934


Do you know what is recursion? Is it possible for a stored procedure to call itself or recursive stored procedure?

982


What type of locking occurs during the snapshot generation? : sql server replication

1317


Do you know what is sql injection?

982


What happens if null values are involved in string operations?

964


How can you list all the columns in a database?

912


What are the ways available in sql server to execute sql statements?

1092


explain what is a deadlock and what is a live lock? How will you go about resolving deadlocks? : Sql server database administration

885