两个日期之间的天数 - ANSI SQL [英] Number of days between two dates - ANSI SQL

查看:296
本文介绍了两个日期之间的天数 - ANSI SQL的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要一种方法来确定SQL中两个日期之间的天数。

I need a way to determine the number of days between two dates in SQL.

答案必须在ANSI SQL中。

Answer must be in ANSI SQL.

推荐答案

ANSI SQL-92将DATE - DATE定义为返回INTERVAL类型。您应该能够使用与使用&ndash从DATE中提取它们相同的方法从INTERVALS中提取标量;适当的– EXTRACT函数(4.5.3)。

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


<提取表达式>在
a datetime或间隔上运行,并返回一个
的精确数值,表示datetime
或间隔的一个组件的
值。

<extract expression> operates on a datetime or interval and returns an exact numeric value representing the value of one component of the datetime or interval.

然而,这在大多数数据库中实施得很差。你可能会使用数据库特定的东西。 DATEDIFF在不同的平台上实施得很好。

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.

这是真正的做法。

SELECT EXTRACT(DAY FROM DATE('2009-01-01') - DATE('2009-05-05')) FROM DUAL;

祝你好运!

这篇关于两个日期之间的天数 - ANSI SQL的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆