ITPub博客

首页 > 数据库 > Oracle > nvl、nvl2与nullif函数用法区别

nvl、nvl2与nullif函数用法区别

原创 Oracle 作者:pwz1688 时间:2014-01-14 16:20:26 0 删除 编辑

NVL (expr1, expr2)->expr1为NULL,返回expr2;不为NULL,返回expr1。注意两者的类型要一致

NVL2 (expr1, expr2, expr3) ->expr1不为NULL,返回expr2;为NULL,返回expr3。expr2和expr3类型不同的话,expr3会转换为expr2的类型。

NULLIF (expr1, expr2) ->相等返回NULL,不等返回expr1。
如下例示:

点击(此处)折叠或打开

  1. SQL> with t as
  2.   2 (select '1999' year, 3000 sales, 245 sales1
  3.   3 from dual
  4.   4 union all
  5.   5 select '2000', 2500, 2500
  6.   6 from dual
  7.   7 union all
  8.   8 select '2001', 4500, null
  9.   9 from dual
  10.  10 union all
  11.  11 select '2002', null, 1200
  12.  12 from dual
  13.  13 union all
  14.  14 select null, 1000, 1100 from dual)
  15.  15 select year,
  16.  16 nvl(sales, 0) nvl_s,
  17.  17 nvl(sales1, 0) nvl_s1,
  18.  18 nvl2(year, sales, sales1) nvl2_s,
  19.  19 nullif(sales, sales1) nullif_s
  20.  20 from t
  21.  21 order by 1;

  22. YEAR NVL_S NVL_S1 NVL2_S NULLIF_S
  23. ---- ---------- ---------- ---------- ----------

  24. 1999 3000  245    3000   3000
  25. 2000 2500  2500   2500
  26. 2001 4500  0      4500   4500
  27. 2002 0     1200
  28.      1000  1100   1100   1000

  29. SQL>


来自 “ ITPUB博客 ” ,链接:https://blog.itpub.net/21251711/viewspace-1070490/ ,如需转载,请注明出处,否则将追究法律责任。

请登录后发表评论 登录
全部评论

注册时间:2009-03-30

  • 博文量
    106
  • 访问量
    736039