在oracle中用like模糊搜索不能搜到空值

来源:互联网 发布:甲骨文 知乎 编辑:程序博客网 时间:2024/05/22 11:58

实例:

SQL> select * from student;        ID NAME                       DEPT---------- -------------------- ----------SQL> insert into student values(1,'小米',1);SQL> insert into student values(2,'',2);SQL> insert into student values(3,null,3);SQL> select * from student  where  name like '%';        ID NAME                       DEPT---------- -------------------- ----------         1 小米                          1SQL> select * from student where name is null;        ID NAME                       DEPT---------- -------------------- ----------         2                               2         3                               3SQL> select * from student where NVL(name,0) like '%';        ID NAME                       DEPT---------- -------------------- ----------         1 小米                          1         2                               2         3                               3

注释:
1、空串对于oracle来说就是null。

2、当name为null时,NVL(name,0)的返回值为0,而0正好符合匹配LIKE '%'这个匹配模式,所以,name为null的数据行可以被查询出来。


1 0