Reputation: 1646
I would like to write a query that returns a single date value, that value being whichever of two columns is the most recent.
So if I have a table with values a, b, date1, date2 I want something like:
SELECT a, b, (Greater of date1 and date2) as date FROM...
Is this possible?
Upvotes: 1
Views: 637
Reputation: 16304
Just us the IF
function...
SELECT a, b,
IF (date1 > date2, date1, date2) AS 'date'
FROM yourtable
... or compare it with GREATEST
:
SELECT a, b,
GREATEST(date1, date2) AS 'date'
FROM yourtable
Upvotes: 0
Reputation: 72636
You can use the GREATEST function .
SELECT a, b, GREATEST(date1,date2) as date FROM...
Upvotes: 6
Reputation: 219824
You can use an IF statement
SELECT a, b, IF(date1>date2, date1, date2) as date
Upvotes: 5