在oracle里,如何取得本周、本月、本季度、本年度的第一天和最后一天的时间

在oracle里,如何取得本周、本月、本季度、本年度的第一天和最后一天的时间


--本周
select trunc(sysdate,'d')+1 from dual;
select trunc(sysdate,'d')+7 from dual;
--本月
select trunc(sysdate,'mm') from dual;
select last_day(trunc(sysdate)) from dual;
--本季
select trunc(sysdate,'Q') from dual;
select add_months(trunc(sysdate,'Q'),3)-1 from dual;
--本年
select trunc(sysdate,'yyyy') from dual;
select add_months(trunc(sysdate,'yyyy'),12)-1 from dual;

=========================================================================

--日期计算,算第n周的第一天及最后一天是几号。  by keynes 2005.04.29

=========================================================================

-- ww的算法为每年1月1日为第一周开始,date+6为每一周结尾

-- 例如20090101为第一周的第一天,而第一周的最后一天为20090101+6=20090107

-- 公式 每周第一天  :date + 周 * 7 - 7

--      每周最后一天  :date + 周 * 7 - 1

-- 如果以ww格式为主,第1、17周的起迄如下

SQL>select to_date('20090101','yyyymmdd') + 1*7-7,to_date('20090101','yyyymmdd') + 1*7-1 from dual;

TO_DATE('20090101','YYYYMMDD')      TO_DATE('20090101','YYYYMMDD')

2009-01-01                                                     2009-01-07

SQL>select to_date('20090101','yyyymmdd') + 17*7-7,to_date('20090101','yyyymmdd') + 17*7-1 from dual;

TO_DATE('20090101','YYYYMMDD')       TO_DATE('20090101','YYYYMMDD')

          2009-04-23                                                      2009-04-29

-- 验证如下

SQL>select to_char(to_date('20090422','yyyymmdd'),'ww') as weekn,

to_char(to_date('20090423','yyyymmdd'),'ww') as week1,

to_char(to_date('20090429','yyyymmdd'),'ww') as week2,

to_char(to_date('20090430','yyyymmdd'),'ww') as weekn2 from dual;

WEEKN    WEEK1     WEEK2    WEEKN2

16               17              17             18

-- iw的算法为星期一至星期日算一周,且每年的第一个星期一为第一周,

-- 例如20090101为星期六,所以用iw的算法是前年的53周,而20050103之后才是第一周的开始。

-- 公式 每周第一天  :next_day(date) + 周 * 7 - 7

--      每周最后一天:next_day(date) + 周 * 7 - 1

-- 如果以iw格式为主,第1、17周的起迄如下

SQL>select next_day(to_date('20090101','yyyymmdd'),'星期一')+ 1 * 7 - 7 as first_day,

          next_day(to_date('20090101','yyyymmdd'),2)+ 1 * 7 - 1 as last_day from dual;

FIRST_DAY      LAST_DAY

--------------------------------------

          2009-01-05         2009-01-11

SQL>select next_day(to_date('20090101','yyyymmdd'),'星期一')+ 17 * 7 - 7 as first_day,

          next_day(to_date('20090101','yyyymmdd'),2)+ 1 7* 7 - 1 as last_day from dual;

FIRST_DAY      LAST_DAY

--------------------------------------

          2009-04-27         2009-05-03

PS:NEXT_DAY   的用法,两个参数第1个是date,第2个是varchar2,返回date

SQL>select   next_day(sysdate,'星期一')   from   dual;

NEXT_DAY(SYSDATE,'星期一')

-------------------   

         2009-11-09 15:20:33

第2个参数可以是数字1-7,分别表示周日到周六

SQL>   select   next_day(sysdate,7)   FROM   DUAL;   

         NEXT_DAY(SYSDATE,7)  

        -------------------   

         2009-11-07 15:19:12

或者更改设置:   

  SQL>   select   NEXT_DAY   (sysdate,   'MONDAY')   FROM   DUAL;   

  select   NEXT_DAY   (sysdate,   'MONDAY')   FROM   DUAL   

  ORA-01846:   周中的日无效  

   

  SQL>   alter   session   set   NLS_DATE_LANGUAGE   =   American;    

  Session   altered  

   

  SQL>   select   NEXT_DAY   (sysdate,   'MONDAY')   FROM   DUAL;      

  NEXT_DAY(SYSDATE,'MONDAY')  

  --------------------------  

  2005-9-26   10:50:25

其它:

----查今天是 "本月" 的第几周

SQL>SELECT TO_CHAR(SYSDATE,'WW') - TO_CHAR(TRUNC(SYSDATE,'MM'),'WW')  AS weekOfMon from dual;



SQL>SELECT TO_CHAR(SYSDATE,'W') AS weekOfMon from dual;

WEEKOFMON

--------------------------

1

----- 查今天是 "今年" 的第几周

select to_char(sysdate,'ww') from dual;



select to_char(sysdate,'iw') from dual;

TO_CHAR(SYSDATE,'IW')

--------------------------

45

附注:

上文所提之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.

--本周

select trunc(sysdate,'d')+1 from dual; select trunc(sysdate,'d')+7 from dual;

--本月

select trunc(sysdate,'mm') from dual; select last_day(trunc(sysdate)) from dual;

--本季

select trunc(sysdate,'Q') from dual; select add_months(trunc(sysdate,'Q'),3)-1 from dual;

--本年

select trunc(sysdate,'yyyy') from dual; select add_months(trunc(sysdate,'yyyy'),12)-1 from dual;

上一篇:滚动标签marquee


下一篇:[HTML]js动态修改表格里面的内容