Oracle 转义


在Oracle中,有些特殊函数的字符是有着特定的含义,这样的话,在检索包含有这样的字符时,就需要进行转移,

总结如下,

1. 通过escape 方式来进行转义,或者是特殊函数的转义' ;

create table t_char(a varchar2(20));
insert into t_char values('a_b');
insert into t_char values('acb');
insert into t_char values('a%b');
insert into t_char values('a''b');
insert into t_char values('a/b');
insert into t_char values('a\b');
insert into t_char values('%');
insert into t_char values('a');
insert into t_char values('_');

测试如下: 


SQL> select * from t_char where a like 'a\_b' escape '\'; 

 
 
 

  
A -------------------- a_b
 
--可见要查询含有通配字符的字符串,必须要使用escape函数
 
 
SQL> select * from t_char where a like 'a\'b' escape '\'; ERROR: ORA-01756: quoted string not properly terminated
SQL> select * from t_char where a like 'a''b' escape '\';
A -------------------- a'b
 
--但是字符'还是需要用两个''来代替字符串中的单引号
 
 
SQL> select * from t_char where a like 'ax_b' escape 'x';
A -------------------- a_b
 
SQL> select * from t_char where a like 'a''_b' escape '''';
A -------------------- a_b
 
SQL> select * from t_char where a like 'a_%b' escape '_';
A -------------------- a%b
 
--可以用其他字符,甚至是“'”、“_”、“%”来用作转义字符,但是一般都使用\,以免发生逻辑错误
 
 
SQL> select * from t_char where a like 'a\/b' escape '\'; select * from t_char where a like 'a\/b' escape '\'                                   * ERROR at line 1: ORA-01424: missing or illegal character following the escape character
SQL> select * from t_char where a like 'a_/b' escape '_'; select * from t_char where a like 'a_/b' escape '_'                                   * ERROR at line 1: ORA-01424: missing or illegal character following the escape character
 
SQL> select * from t_char where a like 'a//b' escape '/';
A -------------------- a/b
 
SQL> select * from t_char where a like 'a/\b' escape '/'; select * from t_char where a like 'a/\b' escape '/'                                   * ERROR at line 1: ORA-01424: missing or illegal character following the escape character
 
SQL> select * from t_char where a like 'a\\b' escape '\';
A -------------------- a\b
 
--要搜索"/"或者"\"则必须使用与之相同的符号作为转义字符,否则无效


  1. 通过escape函数基本上能处理掉常见的转移,但是还有一种比较常见的方式就是通过chr函数,直接通过ascii码的方式来进行转移


              chr()函数将ASCII码转换为字符:字符 –》 ASCII码;

              ascii()函数将字符转换为ASCII码:ASCII码 –》 字符;

              在oracle中chr()函数和ascii()是一对反函数



select * From emp t where t.empno=7839;
-----开始介绍oracle中的条件控制函数的作用
----1,nvl(expr, value)表示expr如何为null的话,就用后面的value替换。否则就进行替换。
----2.nvl2(expr,not null,null) 表示如何expr为null的话就使用第三个参数来替换,否则使用第二的参数来替换
----3.nullif(expr1,expr2) 表示如何expr1==expr2 返回expr1的值,否则返回null
----4.coalesce(expr1,expr2,......,exprn) 表示返回参数列表中第一个不为null的表达式值。
               
select nvl(t.comm,0) as comm,nvl2(t.mgr,'default','null'),
nullif(cast(t.empno as varchar2(10)),t.ename) ,
coalesce(t.comm,t.mgr,t.empno,t.deptno)
From emp t where t.empno=7839;

转载于:https://my.oschina.net/ITBoy/blog/95749

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值