Groups | Blog | Home
all groups > sql server programming > september 2006 >

sql server programming : PATINDEX from right to left?


graham
9/19/2006 9:46:02 PM
I need to extract a substring from the field 'Account' that is always between
parentheses, but is not always the first text between parentheses:

SUBSTRING(Account, PATINDEX('%(%', Account) + 1, PATINDEX('%)%', Account) -
PATINDEX('%(%', Account) - 1)

where Account='Patient (A) Name (PAT1000)'

I'm looking for PAT1000 not A

any suggestions?

Thanks kindly!

Chris Lim
9/19/2006 10:04:02 PM
[quoted text, click to view]

Maybe you can use the REVERSE() function?

REVERSE( SUBSTRING( REVERSE(Account),
PATINDEX('%)%', REVERSE(Account)) + 1,
PATINDEX('%(%', REVERSE(Account)) -
PATINDEX('%)%', REVERSE(Account)) - 1)
)

I'm sure someone else will come up with a more elegant solution though!

Chris
graham
9/19/2006 11:15:02 PM
Thanks, didn't know of the reverse function.

[quoted text, click to view]
Uri Dimant
9/20/2006 12:00:00 AM
Hi
DECLARE @st VARCHAR(50)
SET @st='Patient (A) Name (PAT1000)'

SELECT REVERSE(LEFT(REVERSE(@st), PATINDEX('% %', REVERSE(@st))))





[quoted text, click to view]

AddThis Social Bookmark Button