oracle 计算oracle中两个日期之间的周数

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/36718616/
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-19 03:13:08  来源:igfitidea点击:

To count the number of weeks between two dates in oracle

oracle

提问by renu

I have used the below query to find the number of weeks between two dates:

我使用以下查询来查找两个日期之间的周数:

select  count(week_id) 
from fw 
where week_begin_date >= '2015-01-01' 
and  week_end_date <= '2015-12-31';

Expected result should be 53 but the actual result is 51.

预期结果应为 53,但实际结果为 51。

Kindly help on this.

请对此提供帮助。

回答by xQbert

Can't you just use the week of year function? subtract if needed...

你不能只使用一年中的一周功能吗?如果需要,减去...

 select to_char(to_date('12/31/2015','MM/DD/YYYY'),'WW') from dual;


select To_Number(to_char(to_date('12/31/2015','MM/DD/YYYY'),'WW')) - 
       To_number(to_char(to_date('01/01/2015','MM/DD/YYYY'),'WW')) +1
from dual;

We have to add +1 because weeks start at 1 not 0.

我们必须加上 +1,因为周从 1 开始,而不是 0。

enter image description here

在此处输入图片说明

Now maybe you're after the ISO week format which would be IWinstead of WW.

现在也许你在追求 ISO 周格式,IW而不是WW.

WW:Week of year (1-53) where week 1 starts on the first day of the year and continues to the seventh day of the year.

WW:一年中的第几周 (1-53),其中第 1 周从一年的第一天开始并持续到一年的第七天。

IW:Week of year (1-52 or 1-53) based on the ISO standard.

IW:基于 ISO 标准的一年中的第几周(1-52 或 1-53)。

回答by Asif Mehmood

Did you try this:

你试过这个吗:

SELECT 
  REPLACE(( 
     to_date('20151231','yyyymmdd') - to_date('20150101','yyyymmdd')
  )/7, '-', '')
FROM
  DUAL