Find https:\/\/ and replace with http:\/\/ using phpMyAdmin - mysql

I've got an issue with a find/replace failing in phpmyadmin. I'm thinking this must have to do with the backslashes escaping the slashes in the URL in the database and I'm not correctly accounting for or escaping the backslashes in my query.
The data is stored like this in the wpiv_postmeta table in the meta_value column, with the slashes in the URL escaped:
(more data) [{"ddl-repeat-id":"4","logo":"https:\/\/example.com\/uploads\/2016\/01\/mypng.png"} (more data)
I need to remove the s in the URL. I'm using
UPDATE wpiv_postmeta SET meta_value = replace(meta_value, 'https:\/\/example.com', 'http:\/\/example.com');
but I get no matched rows when I simulate it in phpMyAdmin.
And I don't want to run this because it's too greedy:
UPDATE wpiv_postmeta SET meta_value = replace(meta_value, 'https:', 'http:');
How do I escape or unescape the slashes in my UPDATE query?

You have to double-escape, since the SQL parser removes one level of escapes while processing the query string:
SELECT ... WHERE foo LIKE '%\a%' // find any 'a' in the db
SELECT ... WHERE foo LIKE '%\\a%' // find any '\a' in the db
So you'd need
... REPLACE(meta_evalue, 'http:\\/\\/', 'http://') ...

Related

How to escape slashes in LIKE clause

I need to do some clean-up in a database and change some urls from http://www.domain1.com to http://www.domain2.com, so I am doing a query like this:
UPDATE mytable set url = REPLACE(url, '"http://www.domain1.com"', '"http://www.domain2.com"')
the double comma is needed as this strings are part of a JSON string
however, the problem is that in the DB the old url is for some reason escaped so I have something like this
http:\/\/www.domain1.com
if I run a query like these they don't match anything
select * from mytable where url like '%http://www.domain1.com%'
select * from mytable where url like '%http:\/\/www.domain1.com%'
and therefore the replace query doesn't work either, so what's the correct way to escape these strings?
You might try:
where url like '%http:\\/\\/www.domain1.com%'
To be honest, I don't understand how or why forward slashes would be escaped. Perhaps you are better off just using a wildcard:
where url like '%http:_/_/www.domain1.com%'

PhpMyAdmin SQL Find and replace not working with url that includes escape characters

my wp_postmeta table includes a lot of URLs under the meta_value column which read like:
http:\/\/somename.somehost.com\/~someuser\/sites\/example.com\/
I'd like to change the whole thing to simply https:\/\/example.com\/
How do I do that using Find and replace in PhpMyAdmin?
OR if not possible >> How do I do that with a query?
Are the escape backslashes necessary with the new desired URL as well?
Thanks for any advice :-)
wp_postmeta should not contain any backslashes (escape characters) unless you really want that character. A URL looks like
https://example.com/
not
https:\/\/example.com\/
Are you escaping forward-slash because of some app that needs them escaped?
To remove existing single backslashes, first test via:
SELECT REPLACE(meta_value, '\\\\/', '/')
FROM wp_postmeta
WHERE meta_value LIKE 'http%\\\\/%`
LIMIT 5; -- just check a few
If that fails, change to only \\/ and retest. Similarly change the update below.
Then do something like
UPDATE wp_postmeta
SET meta_value = REPLACE(meta_value, '\\\\/', '/')
WHERE meta_value LIKE 'http%\\\\/%`;

MySql Replace part of a string with escape characters

I'm trying to do a simple update the old url in a database table with a new relative url, but I keep getting "0 rows affected". I think it might have to do with the escape characters in the string?
The URLs in the column 'data' now are structured with the backslash.
Here's my SQL:
UPDATE vjfl_sliderimages
SET data = REPLACE(data, '\/myolddomain.com\/images\/', '\/images\/')
This should work to change the URL of every image from myolddomain.com/images/ to /images/ but for some reason it just doesn't have any affect.
For anyone who has this issue, use CONCAT SQL function with CHAR(92) which correspond to '\' ASCII char.
Example:
UPDATE vjfl_sliderimages SET data = REPLACE(data, CONCAT(CHAR(92), '/myolddomain.com', CHAR(92), '/images', CHAR(92), '/'), CONCAT(CHAR(92), '/images', CHAR(92), '/'))
Escape characters(/) are treated differently in MySQL or SQL VARCHAR fields.
Try
UPDATE vjfl_sliderimages
SET data = REPLACE(date, '////myolddomain.com////images////', '////images////');
If 4 splashes doesn't work then try for 3.
As it is forward slash so no need of escape symbol.
UPDATE vjfl_sliderimages SET data = REPLACE(data,'/myolddomain.com/images/', '/images/');
Edit:-
if the previous url was myolddomain.com/images/
then query must be
UPDATE vjfl_sliderimages SET data = REPLACE(data,'myolddomain.com/images/', '/images/');

How do I escape a colon included in text in a MySQL query

I have moved a WordPress site from a local server to a remote server and a number of the image paths contained in posts are incorrect.
I want to batch change them by running a search/replace query in phpMyadmin but as the text contains a colon it keeps failing. What is the correct syntax to escape the colon and run the query? This is the query I am trying to run.
PDATE wp_post SET post_contect = REPLACE (post_content, ‘old domain:8888', ‘newdomain.co.uk');
Thanks!
The problem isn't the colon (:), it's the fact you're using the wrong quotes - you should use ' to denote a string literal, not ‘.
UPDATE wp_post
SET post_contect =
REPLACE (post_content, 'old domain:8888', 'newdomain.co.uk');

removing characters from field in MS Access database table

Using MS Access 2010.
I have a field in a table that contains windows path names surrounded by quotes, like this
"C:\My Documents\Photos\img1.jpg"
"C:\My Documents\Photos\products\gizmo.jpg"
"C:\My Documents\Photos\img5.jpg"
and so on.
I need to get rid of the quotes so the column looks like this:
C:\My Documents\Photos\img1.jpg
C:\My Documents\Photos\products\gizmo.jpg
C:\My Documents\Photos\img5.jpg
Is there a way to write an update query to do this?
OR a better way to do it altogether?
If you will be doing this from within an Access session, using Access 2000 or later, you can use the Replace() function in an update query to remove the quotes. Remove would mean replace them with an empty string.
UPDATE YourTable
SET path_field = Replace(path_field, '"', '');
If any of those path strings could include quotes within them (yuck!), consider the Mid() function ... ask it to start at the 2nd character (skipping the lead quote), and return the number of characters equivalent to Len(path_field) - 2
UPDATE YourTable
SET path_field = Mid(path_field, 2, Len(path_field) - 2);
Either way, you may want to include a WHERE clause to ignore rows without path_field values.
WHERE Len(path_field) > 0
And if you must do this again when new data is added, use a different WHERE clause to ensure you UPDATE only those rows whose path_field values start and end with quotes.
WHERE path_field Like '"*"'
That was using the * wild card for Access' default ANSI 89 mode. If you will do this from ADO (ANSI 92 mode), use the % wild card.
WHERE path_field Like '"%"'
... or use ALike and the % wild card with either mode.
WHERE path_field ALike '"%"'
The solution with REPLACE already mentioned by others works, but removes ALL quotes, even if they are in the middle of the string.
If you only want to remove quotes at the beginning or at the end, but leave quotes in the middle of the string as they are, you can do it with the following two queries:
Remove first character if it's a quote:
update YourTable
set YourField = right(YourField, len(YourField) - 1)
where left(YourField, 1) = '"'
Remove last character if it's a quote:
update YourTable
set YourTable = left(YourField, len(YourField) - 1)
where right(YourField, 1) = '"'
To make this a permanent change, you might run an update query that looked something like this:
UPDATE [Your Table]
SET [Your Table].[Your Field] = Replace([Your Table].[Your Field],"""","")
This will get rid of all quotes, even if they aren't at the beginning or end. Post back if that's not exactly what you want.
Assuming your column name is MyColumn and table name is MyTable, you can use this sql to update your data to get rid of quotes.
UPDATE MyTable
SET MyColumn = REPLACE(MyColumn,'"','')