Sql Server Coalesce Function

SQL Server: COALESCE Function

In this post explains how to use the COALESCE function in SQL Server (Transact-SQL) with syntax and examples.

Description

In SQL Server (Transact-SQL), the COALESCE function returns the first non-null expression in the list. If all expressions evaluate to null, then the COALESCE function will return null.

z

Syntax

The syntax for the COALESCE function in SQL Server (Transact-SQL) is:

COALESCE( expression1, expression2, ... expression_n )

Parameters or Arguments

expression1 to expression_n

The expressions to test for non-null values.

Applies To

The COALESCE function can be used in the following versions of SQL Server (Transact-SQL):

  • SQL Server 2017, SQL Server 2016, SQL Server 2014, SQL Server 2012, SQL Server 2008 R2, SQL Server 2008, SQL Server 2005

Example

Let's look at some SQL Server COALESCE function examples and explore how to use the COALESCE function in SQL Server (Transact-SQL).

For example:

SELECT COALESCE(NULL, NULL, 'AODBA.com', NULL, 'mySite.com');
Output: 'AODBA.com'

SELECT COALESCE(NULL, 'AODBA.com', 'mySite.com');
Output: 'AODBA.com'

SELECT COALESCE(NULL, NULL, 1, 2, 3, NULL, 4);
Output: 1