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 do you mean by a dependent functionality in a build?
How do I find the query plan in sql server?
How we create SQL Server 2005 Reporting Services ? Give me Sample
Tell me about builtinadministrator?
Can two tables share a primary key?
When I delete any data from a table, does the sql server reduce the size of that table?
Which Model uses the SET concept
Explain the various types of concurrency problem?
What are the export options of ssrs?
What do mean by xml datatype?
How do I run sql server 2014?
What are trace flags and mention a few common trace flags used with sql server?
What is 2nf normalization?
How to Sync Two SQL Azure Databases?
Do you know what are acid properties of transaction?