MySQL Remove everything before third / - mysql

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)

Related

How do you add multiple decimal points to a column in SQL?

I have a column in my table that contains 10-digit hts codes (0000.00.0000). Some of the values do not have the full stop points (0000000000). How can I add the full stop points to all the rows that do not have them?
Edit
The column type is VARCHAR
I want to update all rows where full stop is not present.
I would remove the full stops from all these columns using REPLACE() as part of the update, then you can apply some simple logic using a CONCAT() LEFT(), RIGHT() and SUBSTRING()
to change the simple 0000000000 into 0000.00.0000 like this, rather than trying to identify only the columns without the dots
UPDATE table
set column = CONCAT(
LEFT(REPLACE(column, '.', ''),4),
'.' ,
SUBSTRING(REPLACE(column, '.', ''),5,2),
'.',
RIGHT(REPLACE(column, '.', ''),4)
);
Test it using a select so you do no damage
SELECT some_identifying_column,
CONCAT(
LEFT(REPLACE(column, '.', ''),4),
'.' ,
SUBSTRING(REPLACE(column, '.', ''),5,2),
'.',
RIGHT(REPLACE(column, '.', ''),4)
) as justtesting;
Another approach using insert comes to mind. As others already mentioned, it's a good idea to remove the full stops before inserting them in the 5th and 8th position in the string
select *, insert(insert(replace(hts,'.',''),5,0,'.'),8,0,'.')
from t;

LOCATE everything *after* nth occurrence in string

I have strings which have a JSON-like format, including:
..."id":"500", ..., "id":"600", ...
I need to parse the second id out of the column. I found lots of answers using substring_index, however, I need to get the string after the 2nd (of potentially n) occurrences and not the string before to parse out the ID.
Is there a nice solution?
To find the substring of a column "some_column" occurring after the nth
occurrence of a target string,
SELECT
SUBSTRING(some_column, CHAR_LENGTH(SUBSTRING_INDEX(some_column, <target_string>, <n>)) + <length of target string + 1>)
FROM some_table
-- or if you want to limit the length of your returned substring...
SELECT
SUBSTRING(some_column, CHAR_LENGTH(SUBSTRING_INDEX(some_column, <target_string>, <n>)) + <length of target string + 1>, <desired length>)
FROM some_table
For this question, the form would be:
SELECT
SUBSTRING(col, CHAR_LENGTH(SUBSTRING_INDEX(col, '"id":"', 2)) + 7)
FROM `table`
For now I have:
SELECT substring_index(
substr(col, locate('"id":"', col, locate('"id":"', col) + 6) + 6),
'"',
1)
FROM table
Would love to see a "nicer" answer :-)
In Snowflake, this can be done as follows:
select
, split_part([field_name], '{separator}', {n-counter})
from
[table]
Note: {separator} and {n-counter} are inputs provided by the user. Snowflake requires apostrophes around {separator}.

Replacint part of string from a certain point

i have found some wrong text in 120 record in a table so i have in a varchar field:
'Name rubbish rubbish2 more rubbish'
i want to keep 'Name' en remove all after Name
i have tried this :
SELECT REPLACE(field_name, 'rubbish','') as test
from table
where field_name like '%rubbish%'
but this will ofcourse remove only 'rubbish' not the rest.
I think ther must be a way to remove everything after 5 digits!?
Txs
To remove everything after the first space character:
update mytable set
field_name = substr(field_name, 1, instr(field_name, ' '))
where field_name like '%rubbish%';
See SQLFiddle.
In MySQL, if you want to keep everything before the first space, then you can use substring_index():
update t
set col = substring_index(col, ' ', 1)
where col like '% %';
If you have some set pattern, such as the string 'rubbish', then you can use that. So, this keeps everything before "rubbish":
update t
set col = substring_index(col, 'rubbish', 1)
where col like '%rubbish%';
You can also use this logic in a SELECT statement:
select substring_index(col, 'rubbish', 1)
. . .
If the string does not contain "rubbish", then everything is returned.

How to get left substring in mysql

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.

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.