繁体   English   中英

设置从一年的第一天到下一个星期一的一年的第一周

[英]Set first week of the year from the first day of the year until the following monday

我希望Oracle通过以下方式对待几周:

-一周从星期一开始
-一年的第一周是从一年的第一天到下一个星期一

所以对于2015年

2015年1月1日(星期四)为1
2015年1月2日(星期五)为1
2015年1月3日(星期六)为1
2015年1月4日(星期日)为1
2015年1月5日(星期一)为2
2015年1月6日(星期二)将为2

对于2017年将是

2017年1月1日(星期日)为1
2017年1月2日(星期一)为2
2017年1月3日(星期二)将为2
2017年1月4日(星期三)将为2

等等....

我需要这一年的数字,所以请转到1到53-54

使用ISO周日期标准:“ iw”。

实际上,2014年1月1日是星期三,因此:

select
  to_number(to_char(date'2014-01-05', 'iw')) as weeknum, 
  to_char(date'2014-01-05', 'Day') as day
from dual;

    WEEKNUM DAY
----------- ---------
          1 Sunday

select
  to_number(to_char(date'2014-01-06', 'iw')) as weeknum,
  to_char(date'2014-01-06', 'Day') as day
from dual;

    WEEKNUM DAY
----------- ---------
          2 Monday

一点点算术就可以解决问题(假设d是您的日期):

select TRUNC((case to_char(d, 'DY')
          when 'MON' then 6
          when 'TUE' then 5
          when 'WED' then 4
          when 'THU' then 3
          when 'FRI' then 2
          when 'SAT' then 1
          when 'SUN' then 0
       end + to_number(to_char(d, 'DDD'))-1) / 7)+1
from v;
  • case声明将根据星期几建立补偿
  • 接下来,我添加一年中的某天
  • 然后我将结果除以第7周的数字(从0开始)
  • 最后,+ 1将从1开始编号

请花一些时间尝试一下它http://sqlfiddle.com/#!4/d41d8/38236,以检查我是否没有犯一个愚蠢的错误,因为我没有时间进行认真的测试。 对于那个很抱歉...

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM