SCOTT@OCM11G >select * from test where t like "%\%%" escape "";
T
--------------------
abc%def
(2)再让我们用该方法查询带有’&’符号的数据,结果失败
SCOTT@OCM11G >select * from test where t like "%&%" escape "";
select * from test where t like "%&%" escape ""
*
ERROR at line 1:
ORA-01424: missing or illegal character following the escape character
ORA-01424: 缺失或非法字符转义字符
4.通过&字符的ascii值来解决该问题 (1)查询出’&’字符的ascii值
SYS@OCM11G >select ascii("&") from dual;
ASCII("&")
----------
38
(2)将ascii值代入字符串中(注意使用连接符)
SCOTT@OCM11G >select "abc"||chr(38)||"def" from dual;
"ABC"||
-------
abc&def
SCOTT@OCM11G >insert into test values ("abc"||chr(38)||"def");
1 row created.
SCOTT@OCM11G >commit;
Commit complete.
(3)验证最后插入数据
SCOTT@OCM11G >select * from test;
T
--------------------
abc!def
abc@def
… ...
abc<>def
abc&def --使用&&def插入
abc&def --使用ascii编码插入
21 rows selected.
三、总结 如果想使用特殊’&’字符当字符串来用时,可以使用ascii值来代替,这样可以解决SQL语句中的特殊需求。当然,这种变态的要求很难碰到。Oracle小技巧,有时需要变通的思想,当然我也是借鉴了别的思路。学习不止,Sam继续加油! Where there is a will, there is a way.本文永久更新链接地址