SQL Server COALESCE() Function
Last Updated :
08 Aug, 2024
The COALESCE()
function in SQL Server is a powerful tool designed to handle NULL
values effectively. It evaluates a list of expressions in a specified order and returns the first non-null value encountered.
In this article, We will learn about the SQL Server COALESCE() by understanding various examples in detail.
COALESCE() Function in SQL Server
- The SQL Server COALESCE() function is used to handle NULL values.
- The NULL values are replaced with the user-given value during the expression value evaluation process.
- The SQL Server COALESCE function evaluates the expression in a definite order and always results first not null value from the defined expression list.
Syntax
SQL Server COALESCE() function syntax is:
COALESCE ( exv1, exv2…, exvN )
Explanation:
- exv1, exv2…, exvN are expression values.
- All expressions must have the same data type.
- It could have multiple expressions.
Example of SQL Server COALESCE() Function
Let us look at some examples of the COALESCE() function in SQL server.
Example 1
Let’s Write a query to determine the first non-null value from a list of inputs. Given the inputs NULL
, 'X'
, and 'Y'
, the query should return the first non-null value among them.
SELECT COALESCE (NULL, 'X', 'Y')
AS RESULT ;
Output:
Example 2
Let’s write a SQL query to return the first non-null value from the list. If all values are null, return null. For example, in the query SELECT COALESCE(NULL, 13, 24, 35, 46) AS RESULT
.
SELECT COALESCE (NULL, 13, 24, 35, 46)
AS RESULT ;
Output:
Example 3
Let’s Write a SQL query to demonstrate the use of the `COALESCE` function by selecting the first non-null value from a series of null values, and returning a default string ‘GFG’ when all provided values are null.
SELECT COALESCE (NULL, NULL, NULL, NULL, NULL, 'GFG')
AS RESULT ;
Output:
Example 4
Given a query that uses the `COALESCE` function with a mix of `NULL` values and non-null values, identify the first non-null value returned by the function and explain how `COALESCE` prioritizes values in SQL.
SELECT COALESCE (NULL, NULL, NULL, NULL, 5, 'GFG')
AS RESULT ;
Output:
Example of COALESCE() Function SQL Server Management Studio
Here, we will run the COALESCE() Function in SQL Server Management Studio.
Query:
SELECT COALESCE
(NULL, NULL, NULL, NULL, NULL, 'GFG', 1)
Output:

Example of SQL Server Coalesce function in a string concatenation operation
Let us suppose we have below table name “GeekName”.
F_Name |
M_Name |
L_Name |
Manoj |
M. |
Kumar |
Khushi |
NULL |
Modi |
Payal |
K. |
Chauan |
Nisha |
NULL |
Gupta |
Mina |
NULL |
Singh |
Kishan |
C. |
Maan |
Query:
SELECT F_Name + ' ' +M_Name+ ' '
+ L_Name FullName FROM GeekName ;
Output:
FullName |
Manoj M. Kumar |
NULL |
Payal K. Chauan |
NULL |
NULL |
Kishan C. Maan |
Example of SQL Server COALESCEÂ function to handle the NULL values
The SQL statement will concatenate all three names, but no NULL values will appear in the output.
SELECT F_Name +' '+COALESCE(M_Name, '') +' '
+ L_Name  FullName  FROM GeekName ;
Output :
FullName |
Manoj M. Kumar |
Khushi Modi |
Payal  K. Chauan  |
Nisha Gupta |
Mina Singh |
Kishan C. Maan  |
Important Points About SQL Server COALESCE() Function
- The COALESCE() function in SQL Server is used to handle NULL values effectively by replacing them with user-defined values during expression evaluation.
- All expressions within the COALESCE() function must have the same data type to ensure proper evaluation and return of values.
- The COALESCE() function is available in SQL Server (all supported versions), Azure SQL Database, Azure SQL Managed Instance, Azure Synapse Analytics, and Parallel Data Warehouse.
Conclusion
The COALESCE()
function simplifies the handling of NULL
values in SQL Server by providing a straightforward way to ensure that queries return useful results even when dealing with incomplete or missing data. By evaluating expressions from left to right and returning the first non-null value, COALESCE()
allows for more robust and reliable data retrieval.
Similar Reads
MySQL COALESCE() Function
The MySQL COALESCE() function returns the first non-null value in a list of expressions. COALESCE function in MySQLThe COALESCE function in MySQL is used to get the first non-null value from a list of expressions. If all the values in the list are evaluated to NULL, then the COALESCE() function retu
2 min read
CONCAT() function in SQL Server
CONCAT() : This function in SQL Server helps to concatenate two or more strings together. CONCAT() function can accept a minimum of 2 parameters and a maximum of 254 parameters. Syntax : CONCAT(string_1, string_2, .......string_n) Parameters : string_1, string_2, .......string_n - The given strings
2 min read
CHAR() function in SQL Server
CHAR() : This function helps to convert an int ASCII code to character value i.e if the user pass an integer as an argument, the CHAR() function interpret the integer value and returns its corresponding character value. Syntax : CHAR(integer_value) Parameters : This function accepts only one argumen
2 min read
SQL Server TRY CONVERT() Function
When we deal with databases, we come across different data types. In SQL we have various data types to store different types of data. Like int data type for integers, varchar data type for strings, date data type for storing the data, and XML for XML type data. For such types of data conversions, we
6 min read
CONCAT_WS() Function in SQL Server
CONCAT_WS() : This function concatenates two or more strings together with a separator. Syntax : CONCAT_WS(separator, input_string1, input_string2, [...input_stringN]); Parameter : This method accepts two-parameters as mentioned above and described below as follows. separator - It is an expression o
1 min read
DATEFROMPARTS() Function in SQL Server
DATEFROMPARTS() function : This function in SQL Server is used to return a date from the given values of year, month and day. Features : This function is used to find a date from the stated values of year, month and day. This function comes under Date Functions. This function accepts three parameter
2 min read
SQL Server POWER() Function
The POWER() function in SQL Server is a mathematical function that computes the result of raising a number (the base) to the power of another number (the exponent). It is a versatile function used for various calculations, such as squaring a number, computing roots or applying exponential growth in
4 min read
MySQL CASE() Function
MySQL CASE function is a conditional statement that returns a value when the first condition is met. Once a condition is met, the CASE function does not check for other conditions. If no condition is met it returns the output in ELSE part. CASE Function in MySQLThe CASE Function in MySQL allows usin
4 min read
SQL Server Group Functions
The group function in SQL Server provides a powerful tool for performing calculations on groups of rows, allowing you to group data based on specific criteria. This function is important when you want to analyze and summarize information from multiple records in a data structure. The basic group fun
3 min read
MAX() Function in SQL Server
MAX() : This function in SQL Server is used to find the value that is maximum in the group of values stated. Features : This function is used to find the maximum value.This function comes under Numeric Functions.This function accepts only one parameter namely expression. Syntax : MAX(expression) Par
2 min read