获取Oracle中两个日期之间的天数,包括日期 [英] Get the number of days between two dates in Oracle, inclusive of the dates
问题描述
我想获取两个提供的日期之间的总天数.我已经尝试了以下查询,但并没有完全不同;最后日期不包括在内.
I want to get total number of days between two provided dates. I've tried the below query but didn't get the exact different; the last date is not being included.
select (to_date ('15-06-13','dd-MM-yyyy') - to_date('01-02-12','dd-MM-yyyy'))
from dual
这应该返回501天,但是它返回了500天.如果我在计算后加+1,那么我会得到正确的结果.
This should return 501 days but it is returning 500 days instead. If I add +1 after calculation, then I'm getting the correct result.
我真的需要包含+1还是要获得实际结果的替代方法?
Do I really need to include +1 or is there an alternate approach to get the actual result?
推荐答案
在Oracle中,减去两个日期将返回两个日期之间的天数.
minus
运算符的工作方式与数字相同:
In Oracle substracting two dates returns the number of days between two dates.
A minus
operator works in the same way as for numbers:
20 - 20 = 0 ===> 2013-05-20 - 2013-05-20 = 0
25 - 20 = 5 ===> 2013-05-25 - 2013-05-20 = 5
如果要包含last number
或last date
,则需要添加1:
If you want to include last number
or last date
, you need to add 1:
20 - 20 + 1 = 1 ===> 2013-05-20 - 2013-05-20 + 1 = 1
25 - 20 + 1 = 6 ===> 2013-05-25 - 2013-05-20 + 1 = 6
这篇关于获取Oracle中两个日期之间的天数,包括日期的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!