oracle 获取Oracle中两个日期之间的天数,包括日期

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/20721413/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-10 05:25:52  来源:igfitidea点击:

Get the number of days between two dates in Oracle, inclusive of the dates

oracleoracle11gdate-arithmetic

提问by user968441

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

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.

这应该返回 501 天,但它返回 500 天。如果我在计算后加上 +1,那么我会得到正确的结果。

Do I really need to include +1 or is there an alternate approach to get the actual result?

我真的需要包含 +1 还是有其他方法来获得实际结果?

回答by krokodilko

In Oracle substracting two dates returns the number of days between two dates.
A minusoperator works in the same way as for numbers:

在 Oracle 中减去两个日期返回两个日期之间的天数。
一个minus操作符以同样的方式作为数字:

20 - 20 = 0   ===>      2013-05-20  -  2013-05-20 = 0
25 - 20 = 5   ===>      2013-05-25  -  2013-05-20 = 5

If you want to include last numberor last date, you need to add 1:

如果要包含last numberlast date,则需要添加 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