如何在Oracle数据库中插入特殊字符

时间:2019-02-20 07:41:52

标签: mysql oracle

我正在尝试运行一个简单的insert语句。但是它具有一些特殊字符,这给了我错误。如何对其进行转义以使其按原样插入表中。

update history set comments = 'my testing's' where id= '1323';

4 个答案:

答案 0 :(得分:3)

两个撇号:

update history set comments = 'my testing''s' where id= '1323';
                                         ^^
                                         here

答案 1 :(得分:3)

  

update history set comments = 'my testing's' where id= '1323';

这引发ORA-00933,因为撇号'与单引号相同,我们用它来绑定字符串文字。因此,编译器认为您的字符串在g之后完成,而您得到的错误是因为它不理解以下s的含义。

要解决此问题,我们可以这样避免撇号:

update history set comments = 'my testing''s' where id= '1323';

显式编辑长字符串可能会给您带来麻烦,因此有一种特殊的语法q'[...]'可以转义字符串中的所有引号:

update history set comments = q'[all testing's done by Baba O'Reilly]' 
where id= '1323';

这里是a SQL Fiddle demo

  

如果我有类似@这样的字符,该怎么办?

在这种情况下,@不是特殊字符。无需逃避。

答案 2 :(得分:1)

在Oracle中,您也可以使用q引号:

update history set comments = q'(my testing's' where id= '1323)';

更多信息herehere

答案 3 :(得分:0)

仅仅为了完成图片,另一个有用的选项是CHR(ascii_code)UNISTR(utf_code)函数。

例如:

update history set comments = 'my testing' || chr(39) || 's' where id= '1323';

要添加特殊字符,例如欧元:

update history set comments = 'His salary is ' || unistr('\20ac') || '786,000' where id= '1323';
相关问题