Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

COALESCE and ISNULL are functions in SQL that allow you to replace NULL values with a non-NULL value. The key difference between the two is that COALESCE can take any number of arguments, while ISNULL can only take two.

COALESCE selects the first non-NULL value from a list of arguments:

SELECT COALESCE(col1, col2, col3, 'default') AS res FROM tbl;

This statement will return the first non-NULL value from col1, col2, or col3, or 'default' if all three columns are NULL.

ISNULL, on the other hand, replaces a NULL value with a replacement value:

SELECT ISNULL(col1, 'default') AS res FROM tbl;

This statement will return col1 if it is not NULL, or 'default' if it is NULL.

In summary, COALESCE is used to select the first non-NULL value from a list of arguments, while ISNULL is used to replace a NULL value with a replacement value.