SQL: COALESCE – Return first non-NULL value from multiple choices

Whilst studying for my MCITP Database Developer certification, I have come across a very useful function 1Please ensure you test any script taken from my website on a test/development machine, before running on a production server. that returns the value of the first non-NULL value from multiple choices:

SELECT LastName, FirstName, COALESCE(MobilePhone, HomePhone) AS Phone

If the MobilePhone field is blank, it will use the HomePhone value instead.

The difference between this function and ISNULL, is that COALESCE can accept multiple values:

COALESCE(field1, field2, …, fieldn)

1 Comment

Add a Comment

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.