Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MySQL remove final part of a string after specific character

Tags:

replace

mysql

I need to remove the last part of a string in a column where I have a field named "path" that looks like:

images/prop/images/2034/22399_2034.JPG

I need everything after the last "/" to be deleted, in order to have

images/prop/images/2034/

instead of

images/prop/images/2034/22399_2034.JPG

I have no idea if this is possible. Thanks.

like image 226
ol30cean0 Avatar asked Aug 04 '13 23:08

ol30cean0


1 Answers

You can combine MySQL's TRIM() and SUBSTRING_INDEX() functions:

SELECT TRIM(TRAILING SUBSTRING_INDEX(path, '/', -1) FROM path)
FROM   my_table

See it on sqlfiddle.

like image 76
eggyal Avatar answered Sep 20 '22 22:09

eggyal