Differences Between IsNull() and Coalesce() Functions in SQL Server 2012

This article features the differences between the IsNull() and Coalesce() functions in SQL Server 2012.

In this article, I will explain the differences between the IsNull() and Coalesce() functions in SQL Server 2012. The ISNULL and Coalesce functions are both used to replace null values with a user-defined value. So let's take a look at a practical example of how these functions differ. The example is developed in SQL Server 2012 using the SQL Server Management Studio. There are some differences which are explained below.
 

ISNULL() Function in SQL Server

 
The ISNULL() function is used to replace NULL with the specified replacement value. This function contains only two arguments.
 
Syntax
 
ISNULL (check_exp, change_value)
 

Coalesce() Function in SQL Server

 
The Coalesce() function returns the first non-null value among its arguments. This function doesn't limit the number of arguments, but they must all be of the same data type.
 
Syntax
 
COALESCE ( expression [ ,...n ] )
COALESCE() function is equivalent to the following CASE expression.CASE
WHEN (exp1 IS NOT NULL) THEN exp1
WHEN (exp2 IS NOT NULL) THEN exp2
...
ELSE expN
 

Differences Between IsNull() and Coalesce() Functions

 
1.The COALESCE() function is based on the ANSI SQL standard whereas ISNULL function is a Transact-SQL function
 
2. An expression involving ISNULL with non-null parameters is considered to be NOT NULL, while expressions involving COALESCE with non-null parameters is considered to be NULL.

3. The ISNULL() function contains only two parameters. The COALESCE() function contains multiple parameters. If we use more than two parameters with the ISNULL function then we must use nested ISNULL functions. 
 
Example 1
 
ISNULL() function
  1. SELECT ISNULL(NULLNULL'Hello')  
OUTPUT
 
ISNULL() function in SQL Server
 
COALESCE() function
  1. SELECT COALESCE(NULLNULL'hello')  
OUTPUT
 
COALESCE() function in SQL Server
 
4. The ISNULL() function looks at the first value and the second parameter value is automatically limited to that length but COALESCE() does not have this restriction.
 
Example 2
  1. declare @test varchar(3)  
  2. select isnull(@test, 'ABCD'AS ISNULLResult  
  3. select coalesce(@test, 'ABCD'AS coalesceResult  
OUTPUT
 
ISNULL() function in SQL Server
 
In the above image, the test variable has length 3. So the ISNULL function returns tes and the COALESCE() function does not; depending on the length, it returns test.
 
5. The ISNULL() function contains various types of parameters. The COALESCE() function doesn't limit the number of arguments, but they must all be of the same data type.
 
Example 3
 
ISNULL() function
  1. DECLARE @a VARCHAR(5)='Hello',  
  2. @b INT =5  
  3. SELECT ISNULL(@a, @b) AS ISNULLResult  
OUTPUT
 
ISNULL() function in SQL Server
 
The COALESCE() function:
  1. DECLARE @a VARCHAR(5)='Hello',  
  2. @b INT =5  
  3. SELECT COALESCE(@a, @b) AS COALESCEResult  
OUTPUT
 
COALESCE() function in SQL Server