12

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?

KV Prajapati
  • 93,659
  • 19
  • 148
  • 186
DEVOPS
  • 18,190
  • 34
  • 95
  • 118

2 Answers2

24

Try this one

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

Then the query will become

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

at the Mysql end.

Shakti Singh
  • 84,385
  • 21
  • 134
  • 153
4

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
jensgram
  • 31,109
  • 6
  • 81
  • 98
  • 3
    I also recommend replacing them inside your database: UPDATE tableName SET ColumnName = REPLACE( ColumnName ,'"',"'"); or UPDATE tableName SET ColumnName = REPLACE( ColumnName ,'\"','\\''); – KarlosFontana Aug 05 '14 at 12:12