Hi i need help with a MySQL query.
Is it possible to do some wildcard replace for a string like the example below?
String: example.com/folderone/some/path/to/file.pdf
String: example.com/foldertwo/some/path/to/file.pdf
Replace: newsite.com/some/path/to/file.pdf
newsite.com/some/path/to/file.pdf
newsite.com/some/path/to/file.pdf
To remove the folder and change the domain but keep the path. In this case each folder have a different name with a different length.
something like:
update TABLE set COLUMN = replace(COLUMN, 'example.com/%/', 'newsite.com/');
Using SUBSTRING_INDEX:
UPDATE table1
SET column1 = REPLACE(
column1,
SUBSTRING_INDEX(column1, '/', 2),
'newsite.com'
)
WHERE column1 LIKE 'example.com/%/'
This should honour your subfolder structure.
Related
I have a column with links in my table. Some links have a string like /?source=rss&....
So I want to remove this string /?source=rss and what comes after it.
So if the link looks like this https://website.com/?source=ress&id=123&name=john
It should be https://website.com
I searched about it but only found replace and remove functions, And they won't work as the string is variable and not the same in every link.
I found this UPDATE MyTable SET column = REPLACE(column, ' ', '') WHERE column LIKE '%%'
The database is MYSQL and I'm using SQL queries there
Use substring_index():
UPDATE MyTable
SET column = SUBSTRING_INDEX(column, '/?', 1)
WHERE column LIKE '%/?%';
I have strings that look like this:
#N05/3835412983/academy-of-sciences.html
Where 3835412983 and academy-of-sciences.html will be different throughout the database table called wp_4_posts in a column called post_content.
Is there a wildcard search that I can do to preserve the unique id between the two / and strip *.htm*? Everything to the left of the #05 cannot change. The numbers between the / need to be preserved. And the item after the second / can be stripped entirely.
Doing it manually and there's hundreds that need this done to.
For the above, the end result would be:
#N05/3835412983/
And that result would be written back into the database.
If you want to extract the middle element, then you can use substring_index():
select substring_index(substring_index(str, '/', 2), '/', -1)
This extracts the second element.
Based on the edit, you seem to want:
select concat(substring_index(str, '/', 2), '/')
If you want to update the value, then:
update t
set str = concat(substring_index(str, '/', 2), '/');
update specific string then should use replace() function
Syntax:-
UPDATE table_name
SET column_name =
REPLACE(column_name,'OLD_STRING','NEW_STRING')
WHERE column_name = 'your condtition';
in your case:-
UPDATE wp_4_posts
SET post_content =
REPLACE(post_content,'#N05/3835412983/academy-of-sciences.html','#N05/3835412983/');
if you want result based on condition
UPDATE wp_4_posts
SET post_content =
REPLACE(post_content,'#N05/3835412983/academy-of-sciences.html','#N05/3835412983/')
WHERE column_name = "values";
I have a column in table which is stored in format:
{"field1":"val1","field2":"val4"}
{"field1":"val2","field2":"val5"}
{"field1":"val3","field2":"val6"}
I need to remove all field1 with values(e.g "field1":"val1","field1":"val2","field1":"val3" ) and result should be
{"field2":"val4"}
{"field2":"val5"}
{"field2":"val6"}
I am trying to acheive this via replace but stuck as in '"field1":"val1"' string val1 could be any value like null, some integer.
UPDATE emp SET col = REPLACE(col, '"field1":"val1"', '')
I am stuck due to this dynamic value of val1.
I would prefer to use the JSON_REMOVE function (MySQL) :
UPDATE emp
SET emp.col = JSON_REMOVE(emp.col, '$.field1');
You can also add a WHERE clause :
WHERE emp.col LIKE '%val6%';
References: MySQL JSON_REMOVE and MySQL JSON path
A blog post with examples: MySQL for your JSON
And a note about json path in MySQL:
Propery names in path must be double quoted if the property identifier contains interpunction (spaces, special characters, meta characters) bugs.mysql.com
You can do it like this:
SELECT SUBSTRING(Field, 1, INSTR(Field, '"field1"')) + SUBSTRING(Field, INSTR(Field, '"field2"'), LENGTH(Field)) FROM #Temp
I don't know if this works but this is the idea. (Can't test ATM)
Here is the MsSQL equivalent (works, just tested!):
SELECT SUBSTRING(Field, 0, CHARINDEX('"field1"', Field)) + SUBSTRING(Field, CHARINDEX('"field2"', Field), LEN(Field)) FROM #Temp
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)
Lets say that you have the following stored in table:
{2:22}{4:5}{34:4}
I what to delete {4:5} from this string but the system dosent know what the number after the ":" is just the first one. The query looks something like this:
UPDATE tbl SET this = REPLACE(this,'{4:??}','') WHERE id = 1;
What do i need to put in ?? place to return the following result?
{2:22}{34:4}
Here's one way to do it using LEFT, SUBSTRING, LOCATE and REPLACE:
update yourtable
set yourcolumn =
replace(yourcolumn,
Left(
Substring(yourcolumn,
Locate('{4:',yourcolumn),
Length(yourcolumn)),
Locate('}',Substring(yourcolumn,
Locate('{4:',yourcolumn),
Length(yourcolumn)))),
'')
SQL Fiddle Demo