coalesce和nvl函数

coalesce 函数 :


Oracle COALESCE函数语法为COALESCE(表达式1,表达式2,...,表达式n),n>=2,此表达式的功能为返回第一个不为空的表达式,如果

都为空则返回空值。
注意:所有表达式必须为同一类型或者能转换成同一类型。

返回第一个不为空的


CREATE OR REPLACE VIEW v AS
SELECT NULL AS C1, NULL AS C2,    1 AS C3, NULL AS C4,    2 AS C5, NULL AS C6 FROM DUAL UNION ALL
SELECT NULL AS C1, NULL AS C2, NULL AS C3,    3 AS C4, NULL AS C5,    2 AS C6 FROM DUAL;


SQL> select * from v;

C C	    C3	       C4	  C5	     C6
- - ---------- ---------- ---------- ----------
	     1			   2
			3		      2

SQL> SELECT COALESCE(C1, C2, C3, C4, C5, C6) AS c FROM V;

	 C
----------
	 1
	 3

如果用nvl函数呢?
SELECT nvl(nvl(nvl(nvl(nvl(c1, c2), c3), c4), c5), c6) AS c FROM v;

你可能感兴趣的:(COALESCE)