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 the Ticketing tool used in Wipro technologies at Bangalore...???

0 Answers   Liquid Hub,


Find top Nth employee from each department in terms of salary?

13 Answers   RealPage,


How to retrieve range of 10th rows to 20 th rows from total rows from a database table.? (Not from Dataset)

10 Answers   Infosys, Cognizant,


what is the purpose of creating view is sql server 2000

13 Answers   Wipro,


write a query to remove duplicate records without using primary key column?

2 Answers   ABCO,






What do we have to check in database testing?

0 Answers  


Which stored procedure will you be running to add a linked server?

1 Answers  


Explain sql delete command?

0 Answers  


How To Find That One Week Change in My DataBase we Have Done Like We Have A database Test I Change Table,Stored Procd. Then I Find Which SP,Table We Have Edit/Change in Seven Dayes

1 Answers   INDUS,


hi friends please answer this question ASAP:- how to count the no. of employee in a each department or no. of employee in each location by using emp/dept table

7 Answers   Indecomm,


What are the system database in sql server 2005?

0 Answers  


What are the different methods available under sqlcommand class to access the data?

0 Answers   UGC Corporation,






Categories