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

Where in ms sql server is ’100’ equal to ‘0’?

708


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

578


How to resolve the orphan use problem? : sql server security

615


Characterize join and name diverse sorts of joins?

612


Explain mixed authentication mode of sql server?

626






How to change the data type of an existing column with "alter table" statements in ms sql server?

620


What is sql azure database?

190


What is an indexing strategy?

622


Tell me the phases a transaction has to undergo?

626


What is a select query statement in ms sql server?

671


Mention the command used to rename the database.

628


How to make a remote connection in a database?

603


What are the main sources of data?

699


Explain the Ways to improve the performance of a sql azure database?

72


What are the pros and cons of putting a scalar function in a queries select list or in the where clause?

834