0

I have a column in database and having value like this

course_repeatfkfjkjfjkfer_10_topics_0_presentation_link
course_repeatfkfjfkfkfklfflkflkfs_1_presentation_link
course_repeatfkfjfkfkfklfflkflkfs_2_presentation_link
coursek_epeatfkfjfkfkfklfflkflkfs_10_presentation_link
course_hdhdhhdhdjhdrepeatfkfjfkfkfklfflkflkfs_21_presentation_link

and so on.

I need to pick 0,1,2,10,21, number before _presentation_link , But i need this in mysql as well

i used substr in mysql, but that is not working. Any idea?

Thanks

2 Answers 2

1

One option would be to use a combination of SUBSTRING_INDEX() and REPLACE():

SELECT SUBSTRING_INDEX(REPLACE(col, '_presentation_link', ''), '_', -1)
FROM yourTable

Taking course_repeatfkfjkjfjkfer_10_topics_0_presentation_link as an example, after the replacement, this would become:

course_repeatfkfjkjfjkfer_10_topics_0

The call to SUBSTRING_INDEX() then grabs everything appearing after the final underscore, which is the number you want to capture.

Demo here:

SQLFiddle

Sign up to request clarification or add additional context in comments.

Comments

0

You can use substring_index twice like this:

select substring_index(substring_index(col, '_', -3), '_', 1)
from t

Demo

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.