我需要在一个大表中替换许多不同的字符.由于这些来自Windows-1252字符集,我通过ascii代码引用字符.这些可以在几个列中找到.天真的方法是单独更换每个或替换替代品.
个人更换:
UPDATE table SET name = REPLACE(name, CHR(147), '"'), city = REPLACE(city, CHR(147), '"'); UPDATE table SET name = REPLACE(name, CHR(148), '"'), city = REPLACE(city, CHR(148), '"'); UPDATE table SET name = REPLACE(name, CHR(150), '-'), city = REPLACE(city, CHR(150), '-'); ....
嵌套,如下所述:http://oraclecoder.com/tutorials/oracle-multiple-replace-function--2989.
我很好奇一个人比另一个好.或者,如果有第三种选择甚至更好?我有很多行要迭代,所以任何能提高性能的东西都非常感激.
您可以使用TRANSLATE
函数立即执行此操作:
WITH cte AS ( SELECT '“”–' AS name FROM dual ) SELECT name, TRANSLATE(name, '“”–', '""-') AS result FROM cte;
SqlFiddleDemo
在你的情况下:
UPDATE table SET name = TRANSLATE(name, '“”–', '""-'), city = TRANSLATE(name, '“”–', '""-') -- WHERE REGEX_LIKE(name, '.*(“|”|–).*') -- filter record for update -- OR REGEX_LIKE(city, '.*(“|”|–).*')
从Windows-1252:
147 - “ Left double curved quote 148 - ” Right double curved quote 150 - – En dash