Reputation: 517
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
Upvotes: 0
Views: 27
Reputation: 39467
You can use substring_index
twice like this:
select substring_index(substring_index(col, '_', -3), '_', 1)
from t
Upvotes: 0
Reputation: 521073
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:
Upvotes: 1