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

只谈情不闲聊 提交于 2019-11-26 22:01:18

问题


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.

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


回答1:


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

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


来源:https://stackoverflow.com/questions/20721413/get-the-number-of-days-between-two-dates-in-oracle-inclusive-of-the-dates

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!