Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2008 Forums
 Transact-SQL (2008)
 T-SQL-STRING

Author  Topic 

chinlax
Starting Member

30 Posts

Posted - 2011-10-14 : 00:51:39
Hi All,
There are two questions here, i know to solve in c programming
can u any one help me out to solve in the T-sql please.

1.find the how many times given character repeated in a given string
ex: string='Kirankumar' how many types 'a' repeated in given sting
Ans: 2 times
how many types 'K' repeated in given sting ---ans:2


2. Find the No of Character Accurance in Given String
Eg: Given String "kalsinfosystems"
-- O/P
k : 1 Times
a : 1 Times
l : 1 Times
s : 4 Times
i : 1 Times
n : 1 Times
f : 1 Times
o : 1 T


Thanks in advance

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2011-10-14 : 01:16:00
[code]

1. see illustration below

DECLARE @string varchar(1000),@searchchar char(1)

SELECT @string = 'Kirankumar',@searchchar='k'

SELECT LEN(@string) - LEN(REPLACE(@string,@searchchar,'')) AS Occurance

2.SELECT DISTINCT SUBSTRING(@string,number,1),
LEN(@string) - LEN(REPLACE(@string,SUBSTRING(@string,number,1),''))
FROM master..spt_values
WHERE number BETWEEn 1 AND LEN(@string)
AND type='p'
[/code]

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page
   

- Advertisement -