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


Answers were Sorted based on User's Feedback



How can count the string ? for ex: If i have string like 'bhaskar' then i need like b:1..

Answer / suni soni

In SQL Server 2005 New query window for any database.

declare @CharVal varchar(100),@i int
declare @temp table (CharVal varchar(10))

set @a='Bhaskar'

set @i=1
while (@i<=len(@a))
begin

insert into @temp values (substring(@a,@i,1))

set @i=@i +1

end

select CharVal, count(*) from @temp group by CharVal

Is This Answer Correct ?    10 Yes 2 No

How can count the string ? for ex: If i have string like 'bhaskar' then i need like b:1..

Answer / samba

string s = "bhaskar";
for (int i = 0; i< s.Length; i++)
{
Console.WriteLine("{0}:{1}",s.Substring(i,1),i==2?2:1);
}
Console.ReadLine();

Is This Answer Correct ?    2 Yes 1 No

How can count the string ? for ex: If i have string like 'bhaskar' then i need like b:1..

Answer / deepak

declare @Str As varchar(100)
declare @StrNew As varchar(100)
-- You can set any string (max 100 len)
set @Str = 'Bhaskar'
set @StrNew = @Str
declare @PrintVal varchar(10)


While @StrNew <> ''
BEGIN
set @StrNew = Replace(@Str, left(@Str, 1), '')
set @PrintVal= (left(@Str,1) + cast((len(@Str)-Len
(@StrNew)) as varchar(5)))
print @PrintVal + ' ' + @StrNew +' ' +@Str
set @Str = @StrNew
END

Is This Answer Correct ?    1 Yes 0 No

How can count the string ? for ex: If i have string like 'bhaskar' then i need like b:1..

Answer / sanjay kumar dindaa

Declare @strInput as varchar(20)
declare @intLocal as int
declare @chrLocal as char
DECLARE @strLocal varchar(10)
DECLARE @strFinal varchar(100)

Set @intLocal=1
Set @chrLocal=1
Set @strInput='bhaskar'


If exists (Select 1 from sys.tables where name ='temp')
DROP TABLE temp
Create table temp(chrValue Char(1), Cnt Int)

while (@intLocal < len(@strInput) )
begin
set @chrLocal = Substring(@strInput,@intLocal,1)
IF (Select Count(1) from temp Where chrvalue=@chrLocal)> 0
UPDATE TEMP
Set Cnt=Cnt+1
Where chrValue=@chrLocal
else
Insert Into temp (chrValue, Cnt ) values ( @chrLocal,1)
set @intLocal = @intLocal+1
end


Set @strFinal=''
DECLARE @curLocal CURSOR
SET @curLocal = CURSOR FOR
Select ChrValue + ': '+ Cast(cnt as varchar(2)) from temp

OPEN @curLocal
FETCH NEXT
FROM @curLocal INTO @strLocal
WHILE @@FETCH_STATUS = 0
BEGIN
----PRINT @strLocal
Set @strFinal =@strFinal + ' ' + @strLocal
FETCH NEXT
FROM @curLocal INTO @strLocal
END
CLOSE @curLocal
DEALLOCATE @curLocal
PRINT @strFinal

Output :



(1 row(s) affected)

(1 row(s) affected)

(1 row(s) affected)

(1 row(s) affected)

(1 row(s) affected)

(1 row(s) affected)
b: 1 h: 1 a: 2 s: 1 k: 1

Is This Answer Correct ?    1 Yes 0 No

How can count the string ? for ex: If i have string like 'bhaskar' then i need like b:1..

Answer / pradyumna

declare @temp table
(name char(1),counter int)
declare @str varchar(10)
set @str='bhaskar'

declare @strlen int
set @strlen=len(@str)
declare @ctr int
set @ctr=1

declare @Alpha char(1)

while @ctr<=@strlen
begin
set @alpha=substring(@str,@ctr,@strlen-(@strlen-
@ctr))
if not exists (select 1 from @temp where name=@alpha)
begin
insert into @temp values(@alpha,1)
end
else
begin
update @temp set counter=counter+1 where name=@alpha
end
set @ctr=@ctr+1
end

select * from @temp

Is This Answer Correct ?    1 Yes 0 No

How can count the string ? for ex: If i have string like 'bhaskar' then i need like b:1..

Answer / 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

How can count the string ? for ex: If i have string like 'bhaskar' then i need like b:1..

Answer / debasish

the good away to count is convert it to a arry and the
count the array element.. you can do as you like with array.

Is This Answer Correct ?    0 Yes 1 No

How can count the string ? for ex: If i have string like 'bhaskar' then i need like b:1..

Answer / major

Private Sub Form_Load()
Dim Str As String
Str = "Bhaskar"
Fetch Str
End Sub

Public Sub Fetch(Str As String)
Dim StrNew As String
StrVal1 = Str: StrNew = Str
While StrNew <> ""
StrNew = Replace(Str, Mid(Str, 1, 1), "")
Debug.Print Mid(Str, 1, 1) & Len(Str) - Len(StrNew)
Str = StrNew
Wend
End Sub

Is This Answer Correct ?    1 Yes 4 No

How can count the string ? for ex: If i have string like 'bhaskar' then i need like b:1..

Answer / amit kumar

firstaly take string in any variable. then index with start(')
and end(');then u got the full string length. after that u
substring one character and add (:1) and so on to reach the
last character..

and finally u get result.

bye

Is This Answer Correct ?    3 Yes 7 No

Post New Answer

More SQL Server Interview Questions

What is Index ? Explain its Types?

2 Answers   ADP,


How to delete the duplicate rows from a table in SQL Server ??

3 Answers  


What is query processing?

0 Answers  


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

0 Answers  


syntex of insert

7 Answers  






How to find out name of all employees who has salary less than 200 Rs.?

8 Answers  


How to choose all records from the table?

0 Answers  


What is merge replication?

0 Answers  


What is the difference between function and stored procedure in sql server?

0 Answers  


Explain activity monitors

0 Answers  


Tell me extended events in sql server 2008?

0 Answers  


Explain foreign key in sql server?

0 Answers  


Categories