类别:

条件表达式函数

COALESCE

返回其实参中的第一个非 NULL 表达式;如果其所有实参都是 NULL,则返回 NULL。

语法

COALESCE( <expr1> , <expr2> [ , ... , <exprN> ] )
Copy

使用说明

  • 如果可能,请传入相同类型的实参。避免传入不同类型的实参。

  • 如果其中一个实参是数字,则函数会 强制转换 非数值字符串实参(例如 'a string')和不是类型 NUMBER(18,5) 常量的字符串实参。

    对于不是常量的数值字符串实参,如果 NUMBER(18,5) 不足以表示数值,则应将实参 转换 为可以表示该值的类型。

排序规则详细信息

  • The collation specifications of all input arguments must be compatible.

  • The comparisons follow the collation based on the input arguments' collations and precedences.

  • The collation of the result of the function is the highest-precedence collation of the inputs.

示例

SELECT column1, column2, column3, coalesce(column1, column2, column3)
FROM (values
  (1,    2,    3   ),
  (null, 2,    3   ),
  (null, null, 3   ),
  (null, null, null),
  (1,    null, 3   ),
  (1,    null, null),
  (1,    2,    null)
) v;

+---------+---------+---------+-------------------------------------+
| COLUMN1 | COLUMN2 | COLUMN3 | COALESCE(COLUMN1, COLUMN2, COLUMN3) |
|---------+---------+---------+-------------------------------------|
|       1 |       2 |       3 |                                   1 |
|    NULL |       2 |       3 |                                   2 |
|    NULL |    NULL |       3 |                                   3 |
|    NULL |    NULL |    NULL |                                NULL |
|       1 |    NULL |       3 |                                   1 |
|       1 |    NULL |    NULL |                                   1 |
|       1 |       2 |    NULL |                                   1 |
+---------+---------+---------+-------------------------------------+
Copy
语言: 中文