Sunday, November 9, 2014

SQL Coalesce Example

What does this SQL do?

SELECT COALESCE(FirstName + ' ' + MiddleName, FirstName) + ' ' + LastName FROM Customer

If MiddleName is NULL, then FirstName + '' + MiddleName would be NULL, and so would return FirstName + ' ' + LastName.

If MiddleName is not NULL, it would return FirstName + ' ' +MiddleName + ' ' + LastName