Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using REPLACE with escape characters in phpMyAdmin

I have a database of information that was submitted from a website. The main column data was entered using Markdown, and contains lots of text that looks like this:

Walgreens (www.walgreens.com) is the nation\\\'s largest drugstore chain.

We're switching over to a different wysiwyg editor, and need to clean up the data. I tried doing this in phpMyAdmin:

UPDATE sc_answer
SET answer_text = REPLACE (answer_text, '\\\', '')
WHERE sc_answer_id = 24806

but I get an error:

#1064 - You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ''\\\', '') WHERE sc_answer_id = 24806' at line 3

Can anyone help me out? What do I need to do to get this to work?

like image 850
EmmyS Avatar asked Oct 09 '22 21:10

EmmyS


1 Answers

For each backslash \ in the text field, use 2 backslashes in the SQL. For example, if your table looks like this:

mysql> select * from foo;
+----+---------------------------------------------------------------------------+
| id | bar                                                                       |
+----+---------------------------------------------------------------------------+
|  1 | Walgreens (www.walgreens.com) is the nation\\\'s largest drugstore chain. |
+----+---------------------------------------------------------------------------+
1 row in set (0.00 sec)

then

mysql> update foo set bar = REPLACE(bar,'\\\\\\','') where id = 1;
Query OK, 1 row affected (0.00 sec)
Rows matched: 1  Changed: 1  Warnings: 0

mysql> select * from foo;
+----+------------------------------------------------------------------------+
| id | bar                                                                    |
+----+------------------------------------------------------------------------+
|  1 | Walgreens (www.walgreens.com) is the nation's largest drugstore chain. |
+----+------------------------------------------------------------------------+
1 row in set (0.00 sec)
like image 91
unutbu Avatar answered Oct 12 '22 10:10

unutbu