Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Compare two dates in a table and return the greater using SQL

Both dates are stored in one table. If date1 is greater, I want to return date1, if date2 is greater I want to return date2. I want them to be part of larger query so I would like one main query but if that is not possible, I can use a temp table and use a second query afterward. The code will be executed in a stored procedure.

like image 350
Hammad Khan Avatar asked Jul 08 '11 20:07

Hammad Khan


1 Answers

It'll be a CASE statement in standard SQL

CASE WHEN date1 >= date2 THEN date1 ELSE date2 END

There are specific functions on some RDBMS that will do it like Excel does Max but this is standard...

like image 167
gbn Avatar answered Oct 02 '22 19:10

gbn