Skip to content
Advertisement

Get the last dates from multiple columns

This feels like it should be an easy one. How do I get the latest dates that are in different columns

I would like the result to be:

Advertisement

Answer

Use a CASE expression:

Demo

Note that some databases, such as MySQL, SQL Server, and SQLite, support a scalar greatest function. SQL Server does not, so we can use a CASE expression as a workaround.

Edit:

It appears that in your actual table, one or more of the three date columns could have NULL values. We can adapt the above query as follows:

Demo

User contributions licensed under: CC BY-SA
4 People found this is helpful
Advertisement