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
What is log in sql server?
Difference between LEN() and DATALENGTH() in sql server ?
What is 'Join' and explain its various types.
How to read data in a table with "select" statements?
How to create new tables with "select ... Into" statements in ms sql server?
How to stop a loop early with break statements in ms sql server?
How many databases Microsoft SQL server provides?
What do you mean by recursive stored procedure?
Do you know what is recursion? Is it possible for a stored procedure to call itself or recursive stored procedure?
What type of locking occurs during the snapshot generation? : sql server replication
Do you know what is sql injection?
What happens if null values are involved in string operations?
How can you list all the columns in a database?
What are the ways available in sql server to execute sql statements?
explain what is a deadlock and what is a live lock? How will you go about resolving deadlocks? : Sql server database administration