简体   繁体   English

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

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

I want that Oracle treats weeks the following way: 我希望Oracle通过以下方式对待几周:

-The week starts on monday -一周从星期一开始
-The first week of the year is from the first day of the year until the following monday -一年的第一周是从一年的第一天到下一个星期一

So for 2015 would be 所以对于2015年

01-jan-2015 (Thursday) would be 1 2015年1月1日(星期四)为1
02-jan-2015 (Friday) would be 1 2015年1月2日(星期五)为1
03-jan-2015 (Saturday) would be 1 2015年1月3日(星期六)为1
04-jan-2015 (Sunday) would be 1 2015年1月4日(星期日)为1
05-jan-2015 (Monday) would be 2 2015年1月5日(星期一)为2
06-jan-2015 (Tuesday) would be 2 2015年1月6日(星期二)将为2

For 2017 would be 对于2017年将是

01-jan-2017 (Sunday) would be 1 2017年1月1日(星期日)为1
02-jan-2017 (Monday) would be 2 2017年1月2日(星期一)为2
03-jan-2017 (Tuesday) would be 2 2017年1月3日(星期二)将为2
04-jan-2017 (Wednesday) would be 2 2017年1月4日(星期三)将为2

and so on.... 等等....

I need the numeration for the year so would go to 1 to 53-54 我需要这一年的数字,所以请转到1到53-54

Use the ISO week date standard: 'iw'. 使用ISO周日期标准:“ iw”。

2014-01-01 was Wednesday actually, so: 实际上,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

A little bit of arithmetics will probably do the trick (assuming d is your date): 一点点算术就可以解决问题(假设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;
  • The case statement will build an offset according to the day of week case声明将根据星期几建立补偿
  • next I add the day of year 接下来,我添加一年中的某天
  • then I divide the result by 7 for the week number (starting at 0) 然后我将结果除以第7周的数字(从0开始)
  • finally, the +1 will start numbering weeks at 1 最后,+ 1将从1开始编号

Please take some time to experiment with it http://sqlfiddle.com/#!4/d41d8/38236 to check if I didn't make a stupid mistake as I didn't have time to test it thoughtfully. 请花一些时间尝试一下它http://sqlfiddle.com/#!4/d41d8/38236,以检查我是否没有犯一个愚蠢的错误,因为我没有时间进行认真的测试。 Sorry about that... 对于那个很抱歉...

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

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