Reputation: 3881
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;]*','&');
Upvotes: 7
Views: 27158
Reputation: 1425
Try to execute this one:
UPDATE Tablename
SET columnname = REPLACE(columnname, '&', '&')
WHERE columnname LIKE '%&%'
Upvotes: 3
Reputation: 3881
Following sql will replace &
or &
or &amp
or &amp
or its sequence to &
UPDATE tablename
SET columnname = REPLACE(REPLACE(columnname, '&', '&'), 'amp;', '');
or
UPDATE tablename
SET columnname = REPLACE(columnname , 'amp;', '')
Upvotes: 1
Reputation: 15140
UPDATE tablename SET columnname=REPLACE(REPLACE(columnname,'&','&'), 'amp;', '');
This will first replace "&"
with "&"
, then replace all "amp;"
with ""
(empty string).
Upvotes: 9