laika222
9/1/2017 - 9:26 PM

COALESCE().sql

-- COALESCE searches columns in order and returns the first non-NULL value it finds. Format is COALESCE,column/variable1, column/variable2...) (returns the value of the first non-NULL column or variable in a list)

-- Example below shows how COALESCE will look through the various columns and will pick in the result set the first non-NULL value (if DiscontinuedDate is NULL, it'll move to SellEndDate, and if that's NULL, it'll move to SellStartDate, and if that isn't NULL, it'll display the value of SellStartDate)

SELECT Name, COALESCE(DiscontinuedDate, SellEndDate, SellStartDate) AS LastActivity
FROM orders1;