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

35,577

Solution 1

Try this one

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

Then the query will become

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

at the Mysql end.

Solution 2

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
Share:
35,577
DEVOPS
Author by

DEVOPS

Updated on November 19, 2020

Comments

  • DEVOPS
    DEVOPS over 3 years

    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?

  • KarlosFontana
    KarlosFontana over 9 years
    I also recommend replacing them inside your database: UPDATE tableName SET ColumnName = REPLACE( ColumnName ,'"',"'"); or UPDATE tableName SET ColumnName = REPLACE( ColumnName ,'\"','\\'');