Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to replace all double quotes to single quotes using mysql replace?

I need to replace all double quotes to single quotes using mysql query.

How can I do that. My sql should be in double quotes.

mysql="select replace(text,'\"',''') from mytable"

throwing error. How can I escape that single quotes inside query?

like image 649
DEVOPS Avatar asked Nov 18 '11 12:11

DEVOPS


People also ask

How do you change single quotes to double quotes in SQL?

Backspace over the double quotes and then type a single quote.

How do you replace double quotes?

Use the String. replaceAll() method to replace single with double quotes, e.g. const replaced = str. replaceAll("'", '"'); . The replaceAll method will return a new string where all occurrences of single quotes are replaced with double quotes.

How do you escape double quotes in SQL?

If you need to use the double quote inside the string, you can use the backslash character. Notice how the backslash in the second line is used to escape the double quote characters. And the single quote can be used without a backslash.


2 Answers

Try this one

 $mysql="select replace(text,'\"',\"'\") from mytable";

Then the query will become

select replace(text,'"',"'") from mytable

at the Mysql end.

like image 64
Shakti Singh Avatar answered Sep 23 '22 13:09

Shakti Singh


You need to escape the single quote ' too (see table 8.1):

mysql="select replace(text,'\"','\\'') from mytable"

Thus, the string sent to MySQL will read:

select replace(text,'"','\'') from mytable
like image 24
jensgram Avatar answered Sep 21 '22 13:09

jensgram