Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to replace & to & in SQL?

I have some bad data in my database table. I want to replace all & or & or &amp or &amp to & only.

In java it is working fine. how to do in SQL?

Java:

    String[] names = new String[] { "Ravi Suthar", 
                                    "Ravi & Suthar", 
                                    "Ravi & Suthar",
                                    "Ravi & Suthar",
                                    "Ravi & Suthar" };

    for (String name : names) {
        System.out.println(name.replaceAll("&[amp;]*", "&"));
    }

SQL:

UPDATE tablename SET columnname=REPLACE(columnname,'&[amp;]*','&');
like image 833
2787184 Avatar asked Dec 07 '15 08:12

2787184


2 Answers

UPDATE tablename SET columnname=REPLACE(REPLACE(columnname,'&','&'), 'amp;', '');

This will first replace "&" with "&", then replace all "amp;" with "" (empty string).

like image 188
HoneyBadger Avatar answered Oct 10 '22 19:10

HoneyBadger


Try to execute this one:

UPDATE Tablename
SET columnname = REPLACE(columnname, '&', '&')
WHERE columnname LIKE '%&%'
like image 32
Jason Clark Avatar answered Oct 10 '22 19:10

Jason Clark