Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Quick character count question 2

Status
Not open for further replies.

nice95gle

Programmer
Nov 25, 2003
359
US
Everyone,
How do I count the number of times a character shows up in a string after a certain point?

Example:

My_string ='abcdefghabcdefgabdeabcaba'

I want to count the amount of times the letter 'a' shows up after the letter 'h'

So the answer should be 5


Well Done is better than well said
- Ben Franklin
 
I don't think there's anything built in to do this, so you'd have to write your own function/proc...

first split the string into a list of characters along with thier positions, maybe with a while statement or something, and then query on this list...

--------------------
Procrastinate Now!
 
Please ignore the fact that this is really ugly. [wink]

Code:
[COLOR=blue]declare[/color] @Temp [COLOR=blue]VarChar[/color](100)

[COLOR=blue]Set[/color] @Temp = [COLOR=red]'abcdefghabcdefgabdeabcaba'[/color]

[COLOR=blue]Select[/color] 	Len([COLOR=#FF00FF]Right[/color](@Temp, Len(@Temp) - [COLOR=#FF00FF]CharIndex[/color]([COLOR=red]'h'[/color], @Temp))) - Len([COLOR=#FF00FF]Replace[/color]([COLOR=#FF00FF]Right[/color](@Temp, Len(@Temp) - [COLOR=#FF00FF]CharIndex[/color]([COLOR=red]'h'[/color], @Temp)), [COLOR=red]'a'[/color], [COLOR=red]''[/color]))

-George

Strong and bitter words indicate a weak cause. - Fortune cookie wisdom
 
Thanks George

That's what I did but I used substring instead of charindex.

So I'm not as dumb as I think I am sometimes...LOL


Well Done is better than well said
- Ben Franklin
 
Oh Crowley16 thanks for the effort.

Well Done is better than well said
- Ben Franklin
 
charindex... doh, forgot... :)

--------------------
Procrastinate Now!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top