H
H
Hello12015-11-25 21:38:55
MySQL
Hello1, 2015-11-25 21:38:55

Working with paths in SQL. Is there an easy way?

Example - sqlfiddle.com/#!9/a2612
There is a column with paths to files, for example '/a/b/c/110/h4eheh.jpg' You
need to get only the last folder, extension from it and insert a static string between them . Those. the new name should be '110_somestring.jpg'
Is it possible to write a query without a bunch of nested functions?
PS Please do not offer to implement this in python/php/node.js, etc.

Answer the question

In order to leave comments, you need to log in

2 answer(s)
R
repeat, 2015-11-25
@Hello1

SELECT CONCAT(SUBSTRING_INDEX(SUBSTRING_INDEX('/a/b/c/110/h4eheh.jpg','/',-2),'/',1),'_somestring.',SUBSTRING_INDEX('/a/b/c/110/h4eheh.jpg','.',-1)) as image

to understand where it came from, here is the "source"
SET @q = '/a/b/c/110/h4eheh.jpg';
SET @a1 = SUBSTRING_INDEX(SUBSTRING_INDEX(@q,'/',-2),'/',1);
SET @a2 = SUBSTRING_INDEX(@q,'.',-1);
SELECT @q, CONCAT(@a1,'_somestring.',@a2),@a2

M
Max, 2015-11-25
@MaxDukov

explode by /, then take only the last and third from the end, then glue back together via GROUP_CONCAT.
in principle, nothing prevents the function from being corrected so that it can be glued back together.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question