Link to home
Start Free TrialLog in
Avatar of H-SC
H-SCFlag for United States of America

asked on

Extract everything after a certain character

I have a table that has many rows of data like the following ...
abcdefg_524_test_testtest_32
4_abcdefg_test_testtest_32
abcdefg_test_testtest_382
abcdefg_test_tes_ttest_32

I need to get the set of numbers after the last "_" in the string.  How can this be done?


Avatar of Guy Hengel [angelIII / a3]
Guy Hengel [angelIII / a3]
Flag of Luxembourg image

select substring( yourfield, len(yourfield) - charindex('_', reverse(yourfield))  - 1, len(yourfield)

select REVERSE(LEFT(REVERSE(Col), CHARINDEX('_', REVERSE(Col), 1)-1))
Avatar of H-SC

ASKER

angelIII,

This may work for most of the data, but for some reason a string like : abcdefg_test_testtest_382
 brings back "82"

Avatar of H-SC

ASKER

ewanqoya,

I ran your sql and keep getting the error :
Invalid length parameter passed to the SUBSTRING function.
ASKER CERTIFIED SOLUTION
Avatar of Guy Hengel [angelIII / a3]
Guy Hengel [angelIII / a3]
Flag of Luxembourg image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of H-SC

ASKER

angelIII,

got it, many thanks works great

H-SC
I dont use SUBSTRING in my solution

SELECT REVERSE(LEFT(REVERSE(Col), CHARINDEX('_', REVERSE(Col), 1)-1))
Avatar of H-SC

ASKER

ewanqoya,
hmm, not sure but I indeed still get that exception.

Very interesting

But angelIII's solution works for you already
Avatar of H-SC

ASKER

ewanqoya,

I know, I just like to follow up with everyone who responds to the post out of respect.  many thanks for your help here as well.  take care :)