---
title: nvl2 function
description: Learn the syntax of the nvl2 function of the SQL language in Databricks SQL and Databricks Runtime.
canonical_url: https://docs.databricks.com/aws/en/sql/language-manual/functions/nvl2
---

# `nvl2` function

**Applies to:** ![check marked yes](https://docs.databricks.com/aws/en/images/icons/check.png) Databricks SQL ![check marked yes](https://docs.databricks.com/aws/en/images/icons/check.png) Databricks Runtime

Returns `expr2` if `expr1` is not `NULL`, or `expr3` otherwise. This function is a synonym for `CASE WHEN expr1 IS NOT NULL expr2 ELSE expr3 END`.

## Syntax

```
nvl2(expr1, expr2, expr3)
```

## Arguments

* `expr1`: An expression of any type.
* `expr2`: An expression of any type.
* `expr3`: An expression that shares a [least common type](https://docs.databricks.com/aws/en/sql/language-manual/sql-ref-datatype-rules.md#least-common-type-resolution) with `expr2`.

## Returns

The result is [least common type](https://docs.databricks.com/aws/en/sql/language-manual/sql-ref-datatype-rules.md#least-common-type-resolution) of `expr2` and `expr3`.

Special considerations apply to `VARIANT` types. See [`isnull` function](https://docs.databricks.com/aws/en/sql/language-manual/functions/isnull.md) for details.

## Examples

```sql
> SELECT nvl2(NULL, 2, 1);
 1

> SELECT nvl2('spark', 2, 1);
 2
```

## Related functions

* [`coalesce` function](https://docs.databricks.com/aws/en/sql/language-manual/functions/coalesce.md)
* [`case` expression](https://docs.databricks.com/aws/en/sql/language-manual/functions/case.md)
* [`nvl` function](https://docs.databricks.com/aws/en/sql/language-manual/functions/nvl.md)
