SQL> select 1 from dual where null is null;
1
---------
1
SQL> select 1 from dual where nvl(null,0)=nvl(null,0);
1
---------
1
对空值做加、减、乘、除等运算操作,结果仍为空。
SQL> select 1+null from dual;
SQL> select 1-null from dual;
SQL> select 1*null from dual;
SQL> select 1/null from dual;
查询到一个记录.
注:这个记录就是SQL语句中的那个null
设置某些列为空值
update table1 set 列1=NULL where 列1 is not null;
现有一个商品销售表sale,表结构为:
month char(6) --月份
sellnumber(10,2)--月销售金额
create table sale (month char(6),sell number);
insert into sale values('200001',1000);
insert into sale values('200002',1100);
insert into sale values('200003',1200);
insert into sale values('200004',1300);
insert into sale values('200005',1400);
insert into sale values('200006',1500);
insert into sale values('200007',1600);
insert into sale values('200101',1100);
insert into sale values('200202',1200);
insert into sale values('200301',1300);
insert into sale values('200008',1000);
insert into sale(month) values('200009');(注意:这条记录的sell值为空)
commit;
共输入12条记录
SQL> select * from sale where sell like '%';
MONTH SELL
------ ---------
200001 1000
200002 1100
200003 1200
200004 1300
200005 1400
200006 1500
200007 1600
200101 1100
200202 1200
200301 1300
200008 1000
SQL> select * from sale where sell like '%' or sell is null;
SQL> select * from sale where nvl(sell,0) like '%';
MONTH SELL
------ ---------
200001 1000
200002 1100
200003 1200
200004 1300
200005 1400
200006 1500
200007 1600
200101 1100
200202 1200
200301 1300
200008 1000
200009
create table test (a char(5),b char(5));
SQL> insert into test(a,b) values('1','1');
SQL> insert into test(a,b) values('2','2');
SQL> insert into test(a,b) values('3','');--按照上面的解释,b字段有值的
SQL> insert into test(a) values('4');
SQL> select * from test;
A B
---------- ----------
1 1
2 2
3
4
SQL> select * from test where b='';
----按照上面的解释,应该有一条记录,但实际上没有记录
未选定行
SQL> select * from test where b is null;
----按照上面的解释,应该有一跳记录,但实际上有两条记录。
A B
---------- ----------
3
4
SQL>update table test set b='' where a='2';
SQL> select * from test where b='';
未选定行
SQL> select * from test where b is null;
A B
---------- ----------
2
3
4