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.
| Author |
Topic |
|
Crespo
85 Posts |
Posted - 2002-11-19 : 03:59:19
|
| Is there an SQL function that would return the number of occurences of a particular character in a given string?For example, say I have the sentance 'The cat sat on the mat'.I want a function that would tell me how many times the letter 'a' occurs in the sentance.Now I know how to do this within a loop etc using CHAINDEXs but I was hoping that there is a neat or better way of doing it.Your help would be appreciated.Best Regards.Crespo.Hewitt Bacon & WoodrowEpsomSurreyUnited Kingdom |
|
|
nr
SQLTeam MVY
12543 Posts |
Posted - 2002-11-19 : 04:11:13
|
| Could use recursion but would probably come up against the nest limit.==========================================Cursors are useful if you don't know sql.DTS can be used in a similar way.Beer is not cold and it isn't fizzy. |
 |
|
|
Crespo
85 Posts |
Posted - 2002-11-19 : 04:19:03
|
quote: Could use recursion but would probably come up against the nest limit.==========================================Cursors are useful if you don't know sql.DTS can be used in a similar way.Beer is not cold and it isn't fizzy.
Yes... and that limits the length of the string. You know... I am thinking if using a CUR... CURRRR... Cursor! What do you think? Best Regards.Crespo.Hewitt Bacon & WoodrowEpsomSurreyUnited Kingdom |
 |
|
|
Arnold Fribble
Yak-finder General
1961 Posts |
Posted - 2002-11-19 : 04:34:20
|
SELECT LEN(catcol) - LEN(REPLACE(catcol, 'a', ''))FROM (SELECT 'The cat sat on the mat' AS catcol) a May need to use DATALENGTH depending on considerations about trailing spaces. |
 |
|
|
Crespo
85 Posts |
Posted - 2002-11-19 : 04:43:20
|
quote:
SELECT LEN(catcol) - LEN(REPLACE(catcol, 'a', ''))FROM (SELECT 'The cat sat on the mat' AS catcol) a May need to use DATALENGTH depending on considerations about trailing spaces.
Welldone.... that is a very intersting way of doing it... I did not think of eliminating the As and subtracting the original length. Thanks!Best Regards.Crespo.Hewitt Bacon & WoodrowEpsomSurreyUnited Kingdom |
 |
|
|
Crespo
85 Posts |
Posted - 2002-11-19 : 05:07:00
|
| OK... maybe you can help with the following... I know it can be done in a much simpler way, but i am using a very strange and twsited logic.Right he rs is what I am trying to do....I have lines of addresses..12 Dan-Yr-Heol, Cyncoed, CardiffBakewell House, 32 High Street, Steeple Ashton, Wi2 Bronte Grove, Gaer Estate, Newport61 Preston Avenue, Newport, South WalesNow I need to separate each address into separate parts, i.e. for the first line we would have THREE address partsso bsically where ever a comma occurs we split the address.This is what I have done so far...SELECT ADDRESS, LTRIM(SUBSTRING(ADDRESS, 1, CHARINDEX(',', ADDRESS)-1)), CASE WHEN CHARINDEX(',', ADDRESS, CHARINDEX(',', ADDRESS)+1)-CHARINDEX(',', ADDRESS)-1 > 0 THEN RTRIM(LTRIM(SUBSTRING(ADDRESS,CHARINDEX(',', ADDRESS)+1, CHARINDEX(',', ADDRESS, CHARINDEX(',', ADDRESS)+1)-CHARINDEX(',', ADDRESS)-1))) ELSE '' ENDFROM COGENT_ALLBy the way... COGENT_ALL is the table I am working on... but you only need to be concerned with one column called address.Can you help ?I could do it myself but I am interested in a different approach thats all!Thanks.Best Regards.Crespo.Hewitt Bacon & WoodrowEpsomSurreyUnited Kingdom |
 |
|
|
Robwhittaker
Yak Posting Veteran
85 Posts |
Posted - 2002-11-19 : 05:54:35
|
| Hey, this is my first solution :-)You can do it with a loop and a temp tablecreate table #testtable(test varchar(250))declare @sometext varchar(250)set @sometext = 'a,b,c,d' while len(rtrim(@sometext))>1begin insert into #testtable values (rtrim(LTRIM(SUBSTRING(@sometext, 1, CHARINDEX(',', @sometext)-1)))) set @sometext = substring(@sometext,CHARINDEX(',', @sometext)+1,250)endinsert into #testtable values (rtrim(LTRIM(@sometext))) goselect * from #testtablegodrop table #testtablego |
 |
|
|
Page47
Master Smack Fu Yak Hacker
2878 Posts |
Posted - 2002-11-19 : 07:15:56
|
There are some great articles on this site that deal with working with CSV strings.For example...drop table #testcreate table #test (a varchar(30))insert into #testselect 'followers,of,rambaldi' unionselect 'from,the,eye,to,the,hand' unionselect 'prophet,seer,psychic,alchemist'declare @sep char(1)select @sep = ','select nullif(substring(@sep+a+@sep,n,charindex(@sep,@sep+a+@sep,n)-n),'') as elementfrom toolbox.dbo.numbers cross join #testwhere n<=datalength(@sep+a+@sep) and n-datalength(@sep)>0 and substring(@sep+a+@sep,n-datalength(@sep),datalength(@sep))=@sep and charindex(@sep,@sep+a+@sep,n)-n>0 Jay White{0} |
 |
|
|
|
|
|
|
|