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 are ddl (data definition language) statements for tables in ms sql server?
What are points to remember while using the fillfactor argument?
What are the types of user defined functions in sql server?
How to create percentile function?
how to control the amount of free space in your index pages? : Sql server database administration
How secure is sql server database?
What are triggers? How do you invoke a trigger on demand?
Can we insert data into view sql server?
explain different types of backups avaialabe in sql server? Given a particular scenario, how would you go about choosing a backup plan? : Sql server database administration
What are trace files?
What is the difference between stored procedure and user defined functions?
What are the different SQL Server Versions you have worked on?
You want to check the syntax of a complicated update sql statement without executing it. What command should you use?
what do you understand by change data capture?
What is the use of floor function in sql server?