In SQL, the COALESCE function is used to return the first non-null expression/value in a list of expressions/values.
COALESCE(expression1, expression2, expression3, ...)
• Define the 'expression1', 'expression2', 'expression3' and so on.
1. Consider a sequence of values comprising NULL, NULL, 1, and 2.
2. Run below SQL statement :
SELECT COALESCE(NULL, NULL, 1, 2) AS Result;
3. In this example, the query successfully retrieves the first non-null value which is number '1' from the series of inputs given to COALESCE.
COALESCE in SQL returns the first non-null value among its arguments.
To coalesce two columns in SQL, you use the COALESCE function with those columns as arguments.
The difference between COALESCE() and ISNULL() in SQL is that COALESCE() can take multiple parameters and returns the first non-null value, while ISNULL() is specific to SQL Server and only replaces null values with a specified replacement.
NULLIF in SQL returns null if the two specified expressions are equal, while COALESCE returns the first non-null expression among its arguments.