Compare two dates in a table and return a larger value 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 a larger query, so I need one main query, but if this is not possible, I can use a temporary table and use the second query later. The code will be executed in a stored procedure.

+5
source share
2 answers

It will be a CASE statement in standard SQL

CASE WHEN date1 >= date2 THEN date1 ELSE date2 END

Some DBMSs have certain functions that will do this, as Excel does Max, but this is standard ...

+12
source

SELECT GREATEST('1776-07-04', '1977-08-16'). ( ).

+2

All Articles