簡體   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