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

How to use clusters?

539


What is normalization of database? What are its benefits?

507


You are designing a strategy for synchronizing an SQL Azure database and multiple remote Microsoft SQL Server 2008 databases. The SQL Azure database contains many tables that have circular foreign key relationships?

96


how to avoid cursors? : Sql server database administration

481


What is an indexing technique?

482






What are the types of user defined functions in sql server?

488


How to provide values to stored procedure parameters in ms sql server?

523


What is resource governor?

529


What are subqueries in sql server?

582


You accidentally delete the msdb database what effect does this have on your existing sql databases, and how do you recover?

535


What are the different ways you can create databases in sql server?

555


Do you know the isolation level that sql server support?

512


What happens when converting big values to numeric data types?

554


How would you use user_constraints table in DB?

577


What is the difference between migration and upgradation in sql server?

595