开发者

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

开发者 https://www.devze.com 2023-03-18 20:54 出处:网络
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开发者_JS百科 I would like one mai

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开发者_JS百科 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.


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...


You could SELECT GREATEST('1776-07-04', '1977-08-16'). It should return the most recent date (not necessarily the best date).

0

精彩评论

暂无评论...
验证码 换一张
取 消