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

oracle的nvl函数的用法

2015-08-12 14:45 585 查看
通过查询获得某个字段的合计值,如果这个值位null将给出一个预设的默认值

select nvl(sum(t.dwxhl),1) from tb_jhde t where zydm=-1

这里关心的nvl的用法,nvl(arg,value)代表如果前面的arg的值为null那么返回的值为后面的value

如: NVL(a,b)就是判断a是否是NULL,如果不是返回a的值,如果是返回b的值 通过查询获得某个字段的合计值,如果这个值位null将给出一个预设的默认值

另一个有关的有用方法

declare

i integer

select nvl(sum(t.dwxhl),1) into i from tb_jhde t where zydm=-1

这样就可以把获得的合计值存储到变量i中,如果查询的值为null就把它的值设置为默认的1

Oracle下Nvl函数 nvl( ) 函数 从两个表达式返回一个非 null 值。

语法

NVL(eExpression1, eExpression2)

参数

eExpression1, eExpression2

如 果 eExpression1 的计算结果为 null 值,则 NVL( ) 返回 eExpression2。如果 eExpression1 的计算结果不是 null 值,则返回 eExpression1。eExpression1 和 eExpression2 可以是任意一种数据类型。如果 eExpression1 与 eExpression2 的结果皆为 null 值,则 NVL( ) 返回 .NULL.。

返回值类型

字符型、日期型、日期时间型、数值型、货币型、逻辑型或 null 值

说明

在不支持 null 值或 null 值无关紧要的情况下,可以使用 NVL( ) 来移去计算或操作中的 null 值。 select nvl(a.name,'空得') as name from student a join school b on a.ID=b.ID 注意:两个参数得类型要匹配 问:什么是NULL? 答:在我们不知道具体有什么数据的时候,也即未知,可以用NULL, 我们称它为空,ORACLE中,含有空值的表列长度为零。

ORACLE允许任何一种数据类型的字段为空,除了以下两种情况:

1、主键字段(primary key),

2、定义时已经加了NOT NULL限制条件的字段

说明:

1、等价于没有任何值、是未知数。

2、NULL与0、空字符串、空格都不同。

3、对空值做加、减、乘、除等运算操作,结果仍为空。

4、NULL的处理使用NVL函数。

5、比较时使用关键字用“is null”和“is not null”。

6、空值不能被索引,所以查询时有些符合条件的数据可能查不出来, count(*)中,用nvl(列名,0)处理后再查。

7、排序时比其他数据都大(索引默认是降序排列,小→大), 所以NULL值总是排在最后。

使用方法:

SQL> select 1 from dual where null=null; 没有查到记录

SQL> select 1 from dual where null=''; 没有查到记录

SQL> select 1 from dual where ''=''; 没有查到记录

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

查询到11记录. 结果说明: 查询结果说明此SQL语句查询不出列值为NULL的字段 此时需对字段为NULL的情况另外处理。

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 查询到12记录. Oracle的空值就是这么的用法,我们最好熟悉它的约定,以防查出的结果不正确

来自: http://hi.baidu.com/lonten/blog/item/e75470f004b360a4a50f5267.html
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: