比较表中的两个日期,并使用SQL返回较大的日期 [英] Compare two dates in a table and return the greater using SQL
本文介绍了比较表中的两个日期,并使用SQL返回较大的日期的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
这两个日期都存储在一个表中。如果date1更大,我想返回date1,如果date2更大我想返回date2。我希望他们成为更大的查询的一部分,所以我想要一个主查询,但如果这是不可能的,我可以使用一个临时表,然后使用第二个查询。
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.
推荐答案
这将是标准SQL中的CASE语句
It'll be a CASE statement in standard SQL
CASE WHEN date1 >= date2 THEN date1 ELSE date2 END
在某些RDBMS上有特定的函数,它将像Excel做Max一样,但这是标准的...
There are specific functions on some RDBMS that will do it like Excel does Max but this is standard...
这篇关于比较表中的两个日期,并使用SQL返回较大的日期的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文