MySQL remove final part of a string after specific

2019-06-22 14:11发布

问题:

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.

回答1:

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.



标签: mysql replace