6

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

0

3 Answers 3

4

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, '\\', '#');
Sign up to request clarification or add additional context in comments.

4 Comments

As commented above, 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".
@Jestep I just updated the answer. Do you want to permanently replace the column? or you want only to project it?
I see now. Just ran a test on a few columns. The Update works, but if I simply try to run a select it shows the incorrect output. I was using the select on a static string just to verify the output would be correct.
It's because SELECT and UPDATE are two different things. Anything you do with SELECT doesn't change anything on the database.
0

Try This

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

Comments

-1

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

1 Comment

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"

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.