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

Difference between Inner vs outer joins?

541


What options are there to delete rows on the publisher and not on the subscriber? : sql server replication

693


What is the difference between for xml raw and for xml auto?

579


When I run the sql server 2000 setup, it just hangs. What do I do?

658


What is log cache in sql server?

525






How you trouble shoot when any job fails

1515


What is a dbms wizard?

627


Define self join in sql server joins?

531


What happens if null values are involved in comparison operations?

555


How to execute stored procedure and set temp table in sql server?

564


How do you check sql server is up and running?

527


Where do we use trace frag?

580


What are the built in functions in sql server?

499


Explain primary key in sql server?

575


what are different types of backups available in sql server? : Sql server database administration

505