NVL2

接受三个实参。如果第一个实参不为 NULL,则返回第二个实参,否则返回第三个实参。如果第二个实参与第三个实参的数据类型不一致,则隐式转换为通用类型,类似于 COALESCE

行为类型

不可变

语法

NVL2 ( expression1 , expression2 , expression3 );

参数

  • 如果 expression1 不为 NULL,则 NVL2 返回 expression2。

  • 如果 expression1 为 NULL,则 NVL2 返回 expression3。

注意

实参 2 和 3 可为 Vertica 支持的任何数据类型。

实施等同于 CASE 表达式:

CASE WHENexpression1IS NOT NULL THENexpression2ELSEexpression3 END;

示例

在此示例中,expression1 不为 NULL,因此 NVL2 返回 expression2:

SELECT NVL2('very', 'fast', 'database');
nvl2
------
 fast
(1 row)

在此示例中,expression1 为 NULL,因此 NVL2 返回 expression3:

SELECT NVL2(null, 'fast', 'database');
nvl2
----------
 database
(1 row)

在以下示例中,expression1 (title) 包含 NULL,因此 NVL2 返回 expression3 ("Withheld"),同时将非空值替换为表达式 "Known":

SELECT customer_name, NVL2(title, 'Known', 'Withheld')
as title
FROM customer_dimension
ORDER BY title;
     customer_name      | title
------------------------+-------
 Alexander I. Lang      | Known
 Steve S. Harris        | Known
 Daniel R. King         | Known
 Luigi I. Sanchez       | Known
 Duncan U. Carcetti     | Known
 Meghan K. Li           | Known
 Laura B. Perkins       | Known
 Samantha V. Robinson   | Known
 Joseph P. Wilson       | Known
 Kevin R. Miller        | Known
 Lauren D. Nguyen       | Known
 Emily E. Goldberg      | Known
 Darlene K. Harris      | Known
 Meghan J. Farmer       | Known
 Bettercare             | Withheld
 Ameristar              | Withheld
 Initech                | Withheld
(17 rows)

另请参阅