Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MySQL Replace single backslash

Tags:

mysql

Trying to replace a single backslash in a large db. Was going to run an Update Replace query on one column, but I can't figure out how to replace a single backslash. There was some poor updates done and these slashes need to be changed to another character, they are not escaping anything and do not perform any relevant function.

SELECT
REPLACE (
    "Some\s string\s with slashe\s",
    '\\',
    '  something  '
)

When I run this, the output is: "Somes strings with slashes"

Is there any way to do a true replace on a single slash? No matter what I put in the replace parameter, it just eliminates the single backslash, but doesn't actually replace it. Not sure if this is a bug or I'm just missing something.

I have tried:

SELECT
REPLACE (
    "Some\s string\s with slashe\s",
    '\',
    '  something  '
)

and I get:

[Err] 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 '\')' at line 1

like image 950
Jestep Avatar asked Aug 08 '12 15:08

Jestep


1 Answers

REPLACE( ) doesn't need to be escaped

select replace("Some\s string\s with slashe\s", '\\', '  something  ');

UPDATE 1

I think you want to permanently replace it. Right? Use UPDATE not SELECT

UPDATE tableName
SET    columnName = replace(columnName, '\\', '#');
like image 108
John Woo Avatar answered Oct 07 '22 17:10

John Woo