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 SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

charindex usage

Status
Not open for further replies.

washaw

Programmer
Feb 13, 2008
48
0
0
what is the easy way to get a index number of a third or fourth or fifth space in a string.

using carindex, it is easy to get first and the second, but it gets complicated on the third and afterwards

easy there an easy way to get it

Thanks,
 
Are you doing some sort of data processing on the string or breaking it up?
If you need to get at portions of a string like that, either you "break off" the first part, and then the next one is just waiting for you, or else you use different delimiters, so that you can just dive into where you need.

You're making you life overly complex by trying to spool through that, like that.

If you have to leave it like this then what you can do is something like this:


Create a user defined function that takes the string, the delimiter, the start position in the string to look, and the instance index of the delimiter that you are after. such as the 3rd or 4th.

You will recursively call this function decrimenting the instance index, and incrementing the start point with each call, till you are at one and have the actual index, or else cannot find the string in which case you return nada.

let me know if you want psuedo code, it shouldn't be that bad.

-Sometimes the answer to your question is the hack that works
 
Do you need more help with this?

-Sometimes the answer to your question is the hack that works
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top