This SQL Server tutorial explains how to use the COALESCE function in SQL Server (Transact-SQL) with syntax and examples.
Description
In SQL Server (Transact-SQL), the COALESCE functions returns the first non-null expression in the list. If all expressions evaluate to null, then the COALESCE function will return null.
Syntax
The syntax for the COALESCE function in SQL Servers (Transact-SQL) is:
COALESCE( expression1, expression2, ... expression_n )
Parameters or Arguments
expression1 to expression_nThe expressions to test for non-null values.
Applies To
The COALESCE function can be used in the following versions of SQL Servers (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 functions examples and explore how to use the COALESCE function in SQL Servers (Transact-SQL).
For example:
SELECT COALESCE(NULL, NULL, 'adglob.in', NULL, 'adglob.com'); Result: 'adglob.in' SELECT COALESCE(NULL, 'adglob.in', 'adglob.com'); Result: 'adglob.in' SELECT COALESCE(NULL, NULL, 1, 2, 3, NULL, 4); Result: 1