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。
如下例示:
-
SQL> with t as
-
2 (select '1999' year, 3000 sales, 245 sales1
-
3 from dual
-
4 union all
-
5 select '2000', 2500, 2500
-
6 from dual
-
7 union all
-
8 select '2001', 4500, null
-
9 from dual
-
10 union all
-
11 select '2002', null, 1200
-
12 from dual
-
13 union all
-
14 select null, 1000, 1100 from dual)
-
15 select year,
-
16 nvl(sales, 0) nvl_s,
-
17 nvl(sales1, 0) nvl_s1,
-
18 nvl2(year, sales, sales1) nvl2_s,
-
19 nullif(sales, sales1) nullif_s
-
20 from t
-
21 order by 1;
-
-
YEAR NVL_S NVL_S1 NVL2_S NULLIF_S
-
---- ---------- ---------- ---------- ----------
-
-
1999 3000 245 3000 3000
-
2000 2500 2500 2500
-
2001 4500 0 4500 4500
-
2002 0 1200
-
1000 1100 1100 1000
-
-
SQL>
来自 “ ITPUB博客 ” ,链接:https://blog.itpub.net/21251711/viewspace-1070490/
,如需转载,请注明出处,否则将追究法律责任。