您的位置:首页 > 数据库 > Oracle

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

2017-01-19 09:25 417 查看
实例:

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 小米                          1

SQL> select * from student where name is null;
ID NAME                       DEPT
---------- -------------------- ----------
2                               2
3                               3

SQL> 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的数据行可以被查询出来。

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: