Jestep
Jestep

Reputation: 985

MySQL Replace single backslash

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

Upvotes: 6

Views: 20012

Answers (3)

Imran Luhur
Imran Luhur

Reputation: 477

Try This

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

Upvotes: 0

John Woo
John Woo

Reputation: 263723

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, '\\', '#');

Upvotes: 4

Rahul P
Rahul P

Reputation: 1103

The parameters of replace() don't need escaping:

select replace('A\B\C', '\', '\');

output:

A\\\\B\\C

So this will work:

select Name from T where Name = replace('A\\B\C', '\', '\\');

AS seen HERE

Upvotes: -1

Related Questions