两个日期之间的天数 - ANSI SQL
我需要一种方法来确定 SQL 中两个日期之间的天数。
答案必须是 ANSI SQL。
I need a way to determine the number of days between two dates in SQL.
Answer must be in ANSI SQL.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
ANSI SQL-92 将 DATE - DATE 定义为返回 INTERVAL 类型。您应该能够使用与使用 EXTRACT 函数 (4.5.3) 从 DATE 中提取标量相同的方法从 INTERVALS 中提取标量。
然而,这在大多数数据库中实现得很差。您可能无法使用特定于数据库的东西。 DATEDIFF 在不同平台上的实现都非常好。
这是“真正”的做法。
祝你好运!
ANSI SQL-92 defines DATE - DATE as returning an INTERVAL type. You are supposed to be able to extract scalars from INTERVALS using the same method as extracting them from DATEs using – appropriately enough – the EXTRACT function (4.5.3).
However, this is very poorly implemented in most databases. You're probably stuck using something database-specific. DATEDIFF is pretty well implemented across different platforms.
Here's the "real" way of doing it.
Good luck!
我不记得使用不支持 DATE1-DATE2 和 SQL 92 似乎同意。
I can't remember using a RDBMS that didn't support DATE1-DATE2 and SQL 92 seems to agree.
我相信 SQL-92 标准支持使用“-”运算符减去两个日期。
I believe the SQL-92 standard supports subtracting two dates with the '-' operator.
SQL 92 支持以下语法:
EXTRACT 函数也是 ANSI 的,但 SQL Server 不支持它。示例:
将计算包装在绝对值函数中可确保结果为正值,即使较小的日期是第一个日期。
支持 EXTRACT:
SQL 92 supports the following syntax:
The EXTRACT function is also ANSI, but it isn't supported on SQL Server. Example:
Wrapping the calculation in an absolute value function ensures the value will come out as positive, even if a smaller date is the first date.
EXTRACT is supported on: