MySQL: get MAX or GREATEST of several columns, but with NULL fields
If date1
can never be NULL
, then the result should never be NULL
, right? Then you could use this, if you want NULL
dates be not counted in the calculations (or change the 1000-01-01
to 9999-12-31
, if you want Nulls to count as the "end of time"):
GREATEST( date1 , COALESCE(date2, '1000-01-01') , COALESCE(date3, '1000-01-01') ) AS datemax