5

I am trying to replace escape character with a string but the query is giving me an irrelevant result

eg- char- '\' replace with 'adfc' with below query

SELECT REPLACE("abcdefgh\i","\\", "adfc" );

output - abcdefghi

Desired output - abcdefghadfci

How can i achieve this in mysql?

11
  • The way I see it, if '\\' represents '\' "escaped" then on the first string the '\i' will just represent 'i' "escaped". Maybe try to double '\\' the first string too Commented Feb 16, 2016 at 12:51
  • @sagi I want to achieve something like "REPLACE("abcdefgh\i","\", "adfc" );" but "\" doesn't work as \ escapes the inverted commas and query error comes. Commented Feb 16, 2016 at 12:51
  • @Rakim We are getting the first string from data itself. So we can't change it but wanted to manipulate it later on. Commented Feb 16, 2016 at 12:52
  • did you try to input the first string as an actual variable then instead of testing with strings? Commented Feb 16, 2016 at 12:53
  • like the actual query you are going to use instead of testing with strings SELECT REPLACE(var,"\\", "adfc" ); Commented Feb 16, 2016 at 13:00

2 Answers 2

2

in your my.ini add this line:

sql-mode="NO_BACKSLASH_ESCAPES"

then restart your mysql server, and replace your query with this:

SELECT REPLACE("abcdefgh\i","\", "adfc" );

reference here

Sign up to request clarification or add additional context in comments.

Comments

0

use this:

SELECT REPLACE("abcdefgh\\i","\\", "adfc" );

single escape character will automatically escape character, so you need to put double escape character for remove escape character.

Comments

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.