MySql Replace part of a string with escape characters - mysql

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/');

Related

SQL Data Update Query About

I have a table named testlink and it has url and newtarget columns.
I would like to take the string expressions https://domain1.com/ here in the url column and change all the data in the newtarget column to https://domain1.com/search/?q= pulled string expression.
So briefly;
url columns from https://domain1.com/topic1
will be changed to https://domain1.com/search/?q=topic1 in the newtarget column
There are about 6 thousand different topics (lines) available.
Database: Mysql / Phpmyadmin.
use REPLACE
UPDATE testlink
SET newtarget = REPLACE(url,'https://domain1.com/','https://domain1.com/search/?q=')
MySQL REPLACE() replaces all the occurrences of a substring within a
string.
REPLACE(str, find_string, replace_with)
If you want to conditionally change the value, you can use string manipulations:
update t
set url = concat(left(url, length(url) - length(substring_index(url, '/', -1))), 'q=', substring_index(url, '/', -1))
where url like 'https://domain1.com/%';
This uses substring_index() to get the last part of the string (after the last /). It uses left() to get the first part (based on the length of the last part) and then concatenates the values you want.
Of course, test this logic using a SELECT before implementing an UPDATE.
If you're using MySQL 8, then you'd be able to do that with REGEXP_REPLACE.
For your example, this should work :
SELECT REGEXP_REPLACE('https://domain1.com/topic1','(https:\/\/domain1\.com\/)(.+)','$1search/?q=$2')

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

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://') ...

how to extract querystring values from an URL string stored in MySql and save them in new fields?

I have a field "url" in the table "test" of the MySql database, that contains a URL with querystring like
http://www.mydomain.com?q=xxxx&p=yyyy
I have created two fields "query_q" and "query_p" to store the query values in the table, however I can create a SQL to extract each querystring value from "url" field and save them in the new fields?
You can use LOCATE() and SUBSTR() as Barmar said:
See SQL Fiddle.
update t1 set
q=substr(url, locate('?q=',url)+3, locate('&p=',url)-locate('?q=',url)-3),
p=substr(url, locate('&p=',url)+3, length(url)-locate('&p=',url)-2);
Of course, for this to work, q has to be the first parameter, preceeded by '?' and 'p' the second. But if not, you can figure it out ;)
If you just need a quick & dirty solution, this might help:
UPDATE urls
SET
query_q = SUBSTRING_INDEX(SUBSTRING_INDEX(url, '?q=', -1), '&p=', 1),
query_p = SUBSTRING_INDEX(url, '&p=', -1)
Please see fiddle here.

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,'"','')

mysql - replace ' with single quote in a field

I'm not exactly sure what happened because I migrated from One server to another of the same spec and SQL...
Still in comments and titles the new database shows the characters ' instead of '
and I was wondering if I could ask for help in either replacing ' with '
or if it was simpler just deleting '
Thanks so much...
Steff
You could use MySQL's REPLACE method (look here):
UPDATE
Changed the statement to reflect the OP's naming:
UPDATE database1.vb_ppgal_albums
SET pp_photos = REPLACE(pp_photos, ''', '\'')
Good luck.
The following is the coding that I use to update double-quote in MySQL. I use the REPLACE function. The first parameter is the field_name that I want to have searched, the second is the escaping of the double quote (\") as the search string, followed by escaping of the escape character (\) followed by the double quote, which will insert into the field name (\"). In the table I will now have a measurement of '1/2\"' instead of '1/2"', which was my goal. I hope this helps. (PS, the Where clause is for show, you may not need it.)
UPDATE `table_name`
SET
`field_name` = REPLACE(`field_name`, '\"', '\\"')
WHERE `Id` > 125