Ask Your Question
2

What is the distinction between COALESCE and ISNULL in SQL?

asked 2023-06-03 19:44:04 +0000

david gravatar image

edit retag flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted
2

answered 2023-06-03 20:10:02 +0000

ladyg gravatar image

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.

edit flag offensive delete link more

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account. This space is reserved only for answers. If you would like to engage in a discussion, please instead post a comment under the question or an answer that you would like to discuss

Add Answer


Question Tools

Stats

Asked: 2023-06-03 19:44:04 +0000

Seen: 14 times

Last updated: Jun 03 '23