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

Search for 2nd space with charindex in a condition

Status
Not open for further replies.

Millard

Programmer
Feb 22, 2001
17
0
0
US
How can I get to the 2nd or 3rd space of a condition using
charindex?
 
The third parameter of CHARINDEX is the starting position for the search. Nest a CHARINDEX within a CHARINDEX to start searching after the first (or second) space.

Position of the second space:
CHARINDEX(' ', SearchString,
CHARINDEX(' ',SearchString) + 1) --Start after 1st space

Position of the third space:
CHARINDEX(' ', SearchString,
CHARINDEX(' ', SearchString,
CHARINDEX(' ',SearchString) + 1)

--Angel [rainbow]
-----------------------------------
Every time I lose my mind, I wonder
if it's really worth finding.
 
Oops! Minor correction, but it makes all the difference:

Position of the third space:
CHARINDEX(' ', SearchString,
CHARINDEX(' ', SearchString,
CHARINDEX(' ',SearchString) + 1) + 1)

--Angel [rainbow]
-----------------------------------
Every time I lose my mind, I wonder
if it's really worth finding.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top