Selecting most recent date between two columns

前端 未结 13 1368
再見小時候
再見小時候 2020-12-30 22:15

If I have a table that (among other columns) has two DATETIME columns, how would I select the most recent date from those two columns.

Example:

13条回答
  •  [愿得一人]
    2020-12-30 22:55

    I think the accepted answer is the simplest. However, I would watch for null values in the dates...

    SELECT ID,
           CASE WHEN ISNULL(Date1,'01-01-1753') > ISNULL(Date2,'01-01-1753') THEN Date1
                ELSE Date2
           END AS MostRecentDate
    FROM Table
    

提交回复
热议问题