首页 > 解决方案 > 替换Oracle数据库中的特殊字符

问题描述

我在一个表中有超过 200 000 个数据,其中一列具有特殊字符“Æ??”,文本为 test1234Æ??。如何用“?”替换它 象征?

标签: oracle11goracle-sqldeveloper

解决方案


Æ 有 unicode 00C6。只需谷歌“Æ unicode”即可找到 - 有了这些知识,您就可以使用该UNISTR函数来表示该字符。但是,如果您的客户端支持 unicode 字符,您也可以将该字符粘贴到您的 sql 中,如下所示。

WITH mystrings AS
(SELECT 'found a Æ in this text' as str FROM DUAL UNION ALL
 SELECT 'text is test1234Æ??' FROM DUAL
)
SELECT 
  REPLACE(str,UNISTR('\00c6'),'?') as clean_string,
  REPLACE(str,'Æ','?') as clean_string2,
  REPLACE(str,UNISTR('\00c6??'),'?') as clean_stringnoqm
  FROM mystrings;

CLEAN_STRING            CLEAN_STRING2           CLEAN_STRINGNOQM       
----------------------- ----------------------- -----------------------
found a ? in this text  found a ? in this text  found a Æ in this text 
text is test1234???     text is test1234???     text is test1234?   

如果您只想保留 a-zA-Z 和逗号范围内的字符,则可以使用正则表达式。周围还有很多其他答案,例如这个

WITH mystrings AS 
( SELECT
  'Brand1® is supercool, but I prefer bRand2™ since it supports Æ' AS str
    FROM dual
)
SELECT
  regexp_replace(str,'[^A-Za-z0-9, ]', '?') AS clean_string
  FROM mystrings;

推荐阅读