Author |
Topic |
bhushanhegde
Starting Member
14 Posts |
Posted - 2008-12-18 : 03:39:18
|
Hi,My string is as follows:'hurrycanecathurrycanecatand dod race'I want to find out character 'a' how many times occured in the above string.pls help me. |
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2008-12-18 : 03:49:49
|
DECLARE @a VARCHAR(100)SET @a = 'hurrycanecathurrycanecatand dod race'SELECT LEN(@a) - LEN(REPLACE(@a, 'a', '')) E 12°55'05.63"N 56°04'39.26" |
 |
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
|
Jai Krishna
Constraint Violating Yak Guru
333 Posts |
Posted - 2008-12-18 : 03:53:44
|
DECLARE @cnt INT,@start INTSELECT @cnt = 0,@start =1DECLARE @len INTSELECT @len = len('hurrycanecathurrycanecatand dod race')WHILE(@len>0)BEGINIF (left(substring('hurrycanecathurrycanecatand dod race',@start,len('hurrycanecathurrycanecatand dod race')),1) = 'a')BEGINSELECT @cnt = @cnt +1ENDSELECT @len = @len - 1SELECT @start = @start + 1ENDSELECT @cntJai Krishna |
 |
|
bhushanhegde
Starting Member
14 Posts |
Posted - 2008-12-18 : 03:55:33
|
Hi,is there any sys defined function to do that? like we have substring, charindex |
 |
|
Jai Krishna
Constraint Violating Yak Guru
333 Posts |
Posted - 2008-12-18 : 03:59:48
|
To my Knowledge I have not found any sys function to solve your problemJai Krishna |
 |
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2008-12-18 : 05:53:40
|
See post made 12/18/2008 : 03:49:49 There is no need for a loop... E 12°55'05.63"N 56°04'39.26" |
 |
|
Jai Krishna
Constraint Violating Yak Guru
333 Posts |
Posted - 2008-12-18 : 06:15:01
|
DECLARE @a VARCHAR(100)SET @a = 'hurrycanecathurrycanecatand dod aaaa'SELECT LEN(@a) - LEN(REPLACE(@a, 'a', ''))Not Getting Correct o/p for the above i/pJai Krishna |
 |
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2008-12-18 : 06:25:00
|
Then replace LEN with DATALENGTH...DECLARE @a VARCHAR(100)SET @a = 'hurrycanecathurrycanecatand dod aaaa'SELECT DATALENGTH(@a) - DATALENGTH(REPLACE(@a, 'a', '')) And you still don't need a loop. E 12°55'05.63"N 56°04'39.26" |
 |
|
Jai Krishna
Constraint Violating Yak Guru
333 Posts |
Posted - 2008-12-18 : 06:27:57
|
Its Ok..I just tried differently from ursJai Krishna |
 |
|
|