ITPub博客

首页 > Linux操作系统 > Linux操作系统 > oracle周數計算

oracle周數計算

原创 Linux操作系统 作者:wwd_wang 时间:2009-05-16 16:31:09 0 删除 编辑

http://www.360doc.com/content/061118/14/14474_264138.html

 -- ww的算法为每年1月1日为第一周开始,date+6为每一周结尾
  -- 例如20050101为第一周的第一天,而第一周的最后一天为20050101+6=20050107
  -- 公式 每周第一天 :date + 周 * 7 - 7
  -- 每周最后一天:date + 周 * 7 - 1
  -- 如果以ww格式为主,第1、17周的起迄如下
  127.0.0.1:asdb:WF>select to_date('20050101','yyyymmdd') + 1*7-7,to_date('20050101','yyyymmdd') + 1*7-1 from dual;
  TO_DATE(' TO_DATE('
  --------- ---------
  01-JAN-05 07-JAN-05
  127.0.0.1:asdb:WF>select to_date('20050101','yyyymmdd') + 17*7-7,to_date('20050101','yyyymmdd') + 17*7-1 from dual;
  TO_DATE(' TO_DATE('
  --------- ---------
  23-APR-05 29-APR-05
  Elapsed: 00:00:00.00
  -- 验证如下
  127.0.0.1:asdb:WF>select to_char(to_date('20050422','yyyymmdd'),'ww') as weekn,to_char(to_date('20050423','yyyymmdd'),'ww') as week1,to_char(to_date('20050429','yyyymmdd'),'ww') as week2,to_char(to_date('20050430','yyyymmdd'),'ww') as weekn2 from dual;
  WEEK WEEK WEEK WEEK
  ---- ---- ---- ----
  16 17 17 18
  Elapsed: 00:00:00.00
  127.0.0.1:asdb:WF>
  -- iw的算法为星期一至星期日算一周,且每年的第一个星期一为第一周,
  -- 例如20050101为星期六,所以用iw的算法是前年的53周,而20050103之后才是第一周的开始。
  -- 公式 每周第一天 :next_day(date) + 周 * 7 - 7
  -- 每周最后一天:next_day(date) + 周 * 7 - 1
  -- 如果以iw格式为主,第1、17周的起迄如下
  127.0.0.1:asdb:WF>select next_day(to_date('20050101','yyyymmdd'),'MONDAY')+ 1 * 7 - 7 as first_day,next_day(to_date('20050101','yyyymmdd'),'MONDAY')+ 1 * 7 - 1 as last_day from dual;
  FIRST_DAY LAST_DAY
  --------- ---------
  03-JAN-05 09-JAN-05
  Elapsed: 00:00:00.00
  127.0.0.1:asdb:WF>
  127.0.0.1:asdb:WF>select next_day(to_date('20050101','yyyymmdd'),'MONDAY')+ 17 * 7 - 7 as first_day,next_day(to_date('20050101','yyyymmdd'),'MONDAY')+ 17 * 7 - 1 as last_day from dual;
  FIRST_DAY LAST_DAY
  --------- ---------
  25-APR-05 01-MAY-05
  Elapsed: 00:00:00.00
  127.0.0.1:asdb:WF>
  -- 验证如下
  127.0.0.1:asdb:WF>select to_char(to_date('20050424','yyyymmdd'),'iw') as weekn,to_char(to_date('20050425','yyyymmdd'),'iw') as week1,to_char(to_date('20050501','yyyymmdd'),'iw') as week2,to_char(to_date('20050502','yyyymmdd'),'iw') as weekn2 from dual;
  WEEK WEEK WEEK WEEK
  ---- ---- ---- ----
  16 17 17 18
  Elapsed: 00:00:00.00
  其它:
  --== 查今天是 "本月" 的第几周
  SELECT TO_CHAR(SYSDATE,'WW') - TO_CHAR(TRUNC(SYSDATE,'MM'),'WW') + 1 AS "weekOfMon" from dual;
  或
  SELECT TO_CHAR(SYSDATE,'W') AS "weekOfMon" from dual;
  --== 查今天是 "今年" 的第几周
  select to_char(sysdate,'ww') from dual;
  或
  select to_char(sysdate,'iw') from dual;
  附注:
  上文所提之iw及ww格式在doc内解释如下
  IW = Week of year (1-52 or 1-53) based on the ISO standard
  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.

来自 “ ITPUB博客 ” ,链接:http://blog.itpub.net/12137615/viewspace-598366/,如需转载,请注明出处,否则将追究法律责任。

请登录后发表评论 登录
全部评论

注册时间:2008-05-08

  • 博文量
    236
  • 访问量
    196301