it sems odd, but I can't find a quick and simple way to solve my task:
I have many rows with different absolute paths like:
/application/themes/openbank/images/prettyPhoto/light_square/default_thumbnail.gif
/application/themes/openbank/images/default_thumbnail.png
/application/themes/openbank/images/prettyPhoto/light_square/a_s.pdf
and I want to split them in
parent_path ("e.g. /application/themes/openbank/images/prettyPhoto/light_square/")
file_name ("e.g. default_thumbnail.gif")
NB: file name's lenght and number of separators is variable.
With
SUBSTRING_INDEX('/application/themes/openbank/images/prettyPhoto/light_square/default_thumbnail.gif', '/', -1)
I correctly get the file_name,
but I can't find a way to retrieve the left part.
I tried LEFT(), RIGHT(), and all other mysql's string function with no success
Linuxatico
Try below to get parent_path
select reverse(substring(reverse('/application/themes/openbank/images/prettyPhoto/light_square/default_thumbnail.gif'),instr(reverse('/application/themes/openbank/images/prettyPhoto/light_square/default_thumbnail.gif'),'/')))
select substr( path, 1, instr(path, substring_index( path, '/', -1)) - 1 ) as "PARENT_PATH", substring_index(path, '/', -1 ) as "FILE_NAME" from table1;
Another way would be the combination of LEFT, SUBSTRING_INDEX and CHAR_LENGTH:
My solution takes the left part of the string with length = length(absolute path) - length(file part).
SET #content = '/application/themes/openbank/images/prettyPhoto/light_square/default_thumbnail.gif';
SELECT
LEFT(
#content,
CHAR_LENGTH(#content) - CHAR_LENGTH(SUBSTRING_INDEX(#content, '/', -1))
);
See the three first answers working in this fiddle.
I'm sure there are some other ways too to get the desired result.
Related
I want to find common parent folder in my table images with field path.
For example, in my table i have for this table 6 rows.
1 c:\images\site1\root\img\logo.png
2 c:\images\site1\root\resource\test1.png
3 c:\images\site1\root\resource\test2.png
4 c:\images\site1\root\resource\test3.png
5 c:\images\site1\root\images\background.png
And I want to have "root" folders:
c:\images\site1\root\img\
c:\images\site1\root\resource\
c:\images\site1\root\images\
What is the SQL request to get this, please?
Many thanks.
This can be a bit tricky. If you know that the path does not share the name of the file, you could replace the last part with an empty string:
select distinct replace(path, substring_index(path, '\\', -1), '')
However, this may not always be true.
What you want is everything before the last occurrence of \\. Here is a method using substring_index():
select distinct substring_index(path, '\\',
length(path) - length(replace(path, '\\', '')
)
The difference in lengths is simply counting the number of separator characters in the string. If the path has one separator (i.e. 'a\b'), then the argument to substring_index() is 1, which is what you want.
I am trying to remove everything before the third / in a column. For example: If there is a URL in the coloumn such as
http://www.example.com/example1/example2?=testest123
I would like to remove everything (not including the thrid slash) so i will be left with something like this
/example1/example2?=testest123
I have tried using this but it only removes everything from first "/" and i can't work out how to get it to count to the third then remove.
update table
set column2 = substring(column1, instr(column1, '/') + 1);
Thanks.
To start, you can use the SUBSTRING_INDEX function to get the characters of the string leading up to the third (or fourth slash, in your example) like this:
SELECT SUBSTRING_INDEX(val, '/', 4)
FROM myTable;
You can use the REPLACE() function to remove that substring by replacing it with an empty string, like this:
SELECT REPLACE(val, SUBSTRING_INDEX(val, '/', 4), '')
FROM myTable;
Now, to update your table, simple rewrite the query to set the value to the one above:
UPDATE myTable
SET val = REPLACE(val, SUBSTRING_INDEX(val, '/', 4), '');
NOTE that if there are less than four occurrences of a forward slash, SUBSTRING_INDEX will return the entire string, and therefore completely replacing the entire string by an empty value so you should be very careful when preforming this update.
Here is an SQL Fiddle example with your sample text, and one that I wrote with fewer slashes to demonstrate the last point.
That's a bit messy, but you can try this :
UPDATE table
SET column2 = SUBSTRING(column1,
LOCATE('/', column1,
LOCATE('/', column1,
LOCATE('/', column1))+1)+1)
I'd like to extract the number between NUMBER and ;. So far I can extract the data up to the number, but I don't want anything after the number. e.g.,
SELECT
SUBSTRING(field, LOCATE('NUMBER=', rrule) + 7)
FROM table
Data field:
DATA:PASS=X12;NUMBER=331;FIELD=1
DATA:PASS=X12;NUMBER=2;FOO=BAR;FIELD=1
Desired Output:
331
2
You can use a combination of SUBSTRING_INDEX functions:
SELECT
SUBSTRING_INDEX(
SUBSTRING_INDEX(field, 'NUMBER=', -1),
';',
1)
FROM
tablename
Please see an example fiddle here.
The inner SUBSTRING_INDEX will return everything after the NUMBER= string, while the second will return everything before the ; returned by the inner function.
The following query
SELECT ASSOCIATED_RISK
FROM PROJECT_ISSUES
WHERE FIND_IN_SET('98',ASSOCIATED_RISK);
returns output as
96,98
90,98
but if I use
SELECT ASSOCIATED_RISK
FROM PROJECT_ISSUES
WHERE FIND_IN_SET('96,98',ASSOCIATED_RISK);
it doesn't returns anything.In this case I would like to retrieve the first row.
96,98
Use the AND clause, like this:
SELECT ASSOCIATED_RISK
FROM PROJECT_ISSUES
WHERE FIND_IN_SET('96',ASSOCIATED_RISK)
AND FIND_IN_SET('98',ASSOCIATED_RISK)
Your query is failing because FIND_IN_SET() does not work properly if the first argument contains a comma (",") character. Reference: http://dev.mysql.com/doc/refman/5.0/en/string-functions.html#function_find-in-set. In your case, the first argument is '96,98', so it fails.
Your comment:
is there any other way I can get it in single query instead of framing multiple find_in_set and concat them
As an alternative solution, you can use locate on your ASSOCIATED_RISK value.
Example:
locate( replace( '96,98', ',', '' ), replace( ASSOCIATED_RISK, ',', '' ) )
Edit:
As per Aziz Shaikh comment, we can see that there is a possibility of true result though the search string not existing in the target string.
As an alternative solution, you can replace the search string from target string with an empty string and compare the lengths. If original string's length is grater than new replaced string, then it is a found true result.
Example:
-- this should be greater than 0 for a found true
length( ASSOCIATED_RISK ) > length( replace( ASSOCIATED_RISK, '96,98', '' ) )
This will Give result. see the difference.
SELECT ASSOCIATED_RISK FROM PROJECT_ISSUES WHERE FIND_IN_SET(ASSOCIATED_RISK,'96,98');
Is there an easy way to replace all the text in a VARCHAR 255 column from "300-21-2" to "300-21-02" with one query?
Thank you.
This is basic SQL
UPDATE tablename
SET columnname = '300-21-02'
WHERE columnname = '300-21-2'
If the pattern is always the same NNN-NN-N then what you need is:
update tablex
set column = concat( substr(column,1,7), lpad(substr(column,8),2,'0') )
see it at fiddle:
http://sqlfiddle.com/#!2/f59fe/1
EDIT As the op showed the pattern
update tablex
set column = CONCAT(
substring_index(col, '-',1), '-',
lpad(substring_index(substring_index(col, '-',-2), '-', 1),2,'0'), '-',
lpad(substring_index(col, '-',-1), 2, '0') )
If you like to convert the first set like 300 to 00300 as your pattern you add the lpad as this: lpad(substring_index(col, '-',1),5,'0')
This should be a lot easier if mysql has support to regex replace, but as it hasnt you have to work with the strings:
from this value: '300-02-1'
from substring_index(col, '-',1) I'm getting: 300
from substring_index(substring_index(col, '-',-2), '-', 1) I'm getting 02 I did this because just put the substring_index(col, '-',2) gave me 300-02 so, i got it from right to left (-2) then i get the first
and substring_index(col, '-',-1) it bring me 1 because it gets the value from right to left
Then I just concatenate it all formatting the ones I want.