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..

In SQL Server 2005 New query window for any database.

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

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..

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

 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..

declare @Str As varchar(100)
declare @StrNew As varchar(100)
-- You can set any string (max 100 len)
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..

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

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..

declare @temp table
(name char(1),counter int)
declare @str varchar(10)

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..

In SQL Server 2005 New query window for any database.

declare @char as charchar(20)

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..

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..

Dim Str As String
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..

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

More SQL Server Interview Questions

How does stuff differ from the replace function?

Which autogrowth database setting is good?

How to display n-1 columns from n number of columns, from a single table in MS SQL server 2005?

Explain the properties of a relational table?

What is model database? : SQL Server Architecture

How to a Query to copy data from on table to another table.

What happens if time-only values are provided as date and time literals?

Selet all the data from table where last name is n of employee

What are joins in sql and what are the different types of joins?

what is a default tcp/ip socket assigned for sql server? : Sql server database administration

What are the types of indexes available with SQL Server?

event classes in sql server

Categories