java 如何更换 & 到 & 在 SQL 中?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/34129059/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to replace & to & in SQL?
提问by 2787184
I have some bad data in my database table. I want to replace all &
or &
or &amp
or &amp
to &
only.
我的数据库表中有一些错误的数据。我要全部更换&
或&
或&amp
或&amp
到&
而已。
In java it is working fine. how to do in SQL?
在java中它工作正常。在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;]*','&');
采纳答案by HoneyBadger
UPDATE tablename SET columnname=REPLACE(REPLACE(columnname,'&','&'), 'amp;', '');
This will first replace "&"
with "&"
, then replace all "amp;"
with ""
(empty string).
这将首先替换"&"
为"&"
,然后全部替换"amp;"
为""
(空字符串)。
回答by Jason Clark
Try to execute this one:
尝试执行这个:
UPDATE Tablename
SET columnname = REPLACE(columnname, '&', '&')
WHERE columnname LIKE '%&%'
回答by 2787184
Following sql will replace &
or &
or &amp
or &amp
or its sequence to &
以下 sql 将替换&
or&
或&amp
or&amp
或其序列为 &
UPDATE tablename
SET columnname = REPLACE(REPLACE(columnname, '&', '&'), 'amp;', '');
or
或者
UPDATE tablename
SET columnname = REPLACE(columnname , 'amp;', '')