mysql: replace \ (backslash) in strings

user1316758 picture user1316758 · Apr 8, 2012 · Viewed 28.4k times · Source

I am having the following problem:

I have a table T which has a column Name with names. The names have the following structure:

A\\B\C

You can create on yourself like this:

create table T ( Name varchar(10));

insert into T values ('A\\\\B\\C');

select * from T;

Now if I do this:

select Name from T where Name = 'A\\B\C';

That doesn't work, I need to escape the \ (backslash):

select Name from T where Name = 'A\\\\B\\C';

Fine.

But how do I do this automatically to a string Name?

Something like the following won't do it:

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

I get: A\\\BC

Any suggestions?

Many thanks in advance.

Answer

Rahul picture Rahul · Apr 1, 2013

You have to use "verbatim string".After using that string your Replace function will look like this

Replace(@"\", @"\\")

I hope it will help for you.