Escaping special characters in phpMyAdmin query - mysql

I am trying to replace the string 1"/850 in the description field of a Magento database with 1-inch/850. I have come up with some variations of this
update catalog_product_entity_text set value = replace(value, ‘1"/850’, ‘1-inch/850’);
I have tried breaking with something like 1\"/850 or other variations and I keep getting an error message. Any suggestions as to the right syntax to use in this case?

It looks like you are using curly quotes. MySQL only understands the straight ones, so write '1"/850' and '1-inch/850'.
If this is not the issue, what error message do you get?

Related

Get rid of %C2%92 characters in MySQL

I'm encountering the character %C2%92 in a bunch of MySQL records and looking to replace it with a single quote.
Something like this:
update
tablename
set field = replace(field, UNHEX('92'), "'")
where field RLIKE UNHEX('92')
Any ideas on how to actually accomplish this?
What comes right before/after the C292? Although that looks like a UTF-8 encoding, I don't recognize it.
You really should go back to where the data came from and avoid "url-encoding" the string. (cf PHP's urlencode()). If you are using PHP, then urldecode() will undo the mess: Fetch the row, field = urlencode(field);, then UPDATE the row in the database.

MySQL Workbench "->" Support

It looks like MySQL Workbench is now supporting JSON functionality, however I'm still seeing parser errors on MySQL 5.7.9 functionality, such as the "->" operator.
When I use the following query, I'm getting a syntax error over the "$.test" portion:
Record
record: {"test": 123}
Query
SELECT test->"$.test" FROM table
The query still executes successfully, however I'm curious as to why the syntax parser is incorrectly showing an error.
OK, the problem is probably something else than what I posted in my comments. You are using double quotes, which represent strings only if the ANSI quotes are not enabled (then they wrap identifiers). Use single quotes instead.

Rails 4 LIKE query with array conditions - apostrophe is escaped with double backslash

Having followed some tips on escaping apostrophes I am getting an unexpected combination of escape characters in the resulting sql statement. The following rails 4 active record statement is run against 5.5.42-MariaDB:
User.where(["surname LIKE ?", "%#{params[:search]}%"])
Where
params[:search] = "O'Keefe"
A .to_sql generates
SELECT * FROM users WHERE surname LIKE '%O\\'Keefe%'
MySQL/MariaBD expects an apostrophe to be escaped as two single apostrophes '' , or with a single backslash \' so this results in a syntax error. I am looking for help to understand why two backslashes \\' are appearing, and for a solution that will maintain protection against SQL injection.
UPDATE
After further investigation following suggestions below, it appears as though the console .to_sql output SELECT * FROM users WHERE surname LIKE '%O\\'Keefe%' is not what is passed onto MySQL. It failed for me 'cos I simply copied the statement into a mysql console to test execution. There is some black magic on route to the database that converts the double backslash \\' into a valid mysql escape sequence.
So problem 1/2 solved
User.where(["surname LIKE ?", "%#{params[:search]}%"])
is valid syntax that correctly auto-escapes the user input string. But can anyone shed any light on the reason for the generation of the double backslash and how it is modified on its way to database execution?
Try this:
User.where(["surname LIKE ?", "%#{params[:search].gsub("'", "''")}%"])
http://dev.mysql.com/doc/refman/5.0/en/string-literals.html#character-escape-sequences

Mysql update field

I want to update a table's field where said field is equal to something.
For example, I want to do:
UPDATE users SET name = 'John' WHERE name = 'Jane'
Is this valid syntax or will I get an error?
Yes, that should work just fine. I would have thought that the use of double-quotes (instead of single quotes) would have thrown an error, but I just tried it and it worked.
Even though double-quotes will work, the use of the single-quotes is the accepted standard. It's a good idea to get used to using that, just in case you find yourself using an RDBMS (Oracle or MSSQL) that more-tightly enforces ANSI SQL.

Replacing a formatted string in MySql

I'm trying to replace all instances of an old BB tag markup in a MySql database with a newer, slightly different one.
The old format is this...
[youtube:********]{Video ID}[/youtube:********]
Which I would like to replace with this...
[youtube:********]http://www.youtube.com/watch?v={Video ID}[/youtube:********]
Where the *'s are a random string of alpha-numeric characters. So simply REPLACE(feild, '[youtube:********]', '[youtube:********]http://www.youtube.com?watch?v= won't do unfortunately.
All the clumsy attempts I've made using REPLACE() and INSTR() have resulted in nasty things like [b]Bold Text[/b]http://www.youtube.com/watch?v=
Is there a way to do this kind of pattern replacement in MySql? Possibly with Regular Expressions?
Thank you.
Is this what you tried?
UPDATE table SET Field = REPLACE(Field,']{',']http://www.youtube.com/watch?v={')
This would depend if there isnt any other occurences of ']{'
EDIT: You may also want to try:
UPDATE table SET Field = LEFT(Field,#) + 'http://www.youtube.com/watch?v='+
RIGHT(Field,(Char_Length(Field)-#);
Just check the syntax with MYSQl docs. Char_LNEGTH() may need to be LENGTH() - im sure you get the idea