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
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 |
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 |
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 |
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 |
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 |
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 |
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 |
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 |
How do you know if sql server is running on your local system?
While using a cursor, how can you differentiate between a deleted row and a row that has been inserted with null data values?
What is the log shipping?
Can you explain what are the restrictions applicable while creating views? : SQL Server Architecture
What is the purpose of the master database?
How much does sql server 2016 cost?
What is a transactions?
What are sparse columns?
You are doing log shipping due to some reasons it is failing. How you will proceed from there
What is a virtual table in sql?
What do you mean by an execution plan? How would you view it?
What is the status of services on passive node for failover cluster in sql server? : sql server database administration
Oracle (3259)
SQL Server (4518)
MS Access (429)
MySQL (1402)
Postgre (483)
Sybase (267)
DB Architecture (141)
DB Administration (291)
DB Development (113)
SQL PLSQL (3330)
MongoDB (502)
IBM Informix (50)
Neo4j (82)
InfluxDB (0)
Apache CouchDB (44)
Firebird (5)
Database Management (1411)
Databases AllOther (288)