简体   繁体   中英

MySQL remove final part of a string after specific character

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.

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 .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM