ORACLE取周、月、季、年的開始時間和結束時間

來源:互聯網
上載者:User

1           取周的開始時間和結束時間

  • 取周的開始時間,以星期一為開始。

SQL>SELECT TRUNC(TO_DATE('2013-11-25 10:31:11','YYYY-MM-DD HH24:MI:SS'),'D') + 1 ASA FROM DUAL;

A

-----------

2013/11/25

 

  • 取周的結束時間,以星期日為結束

SQL>SELECT TRUNC(TO_DATE('2013-11-25 10:31:11','YYYY-MM-DD HH24:MI:SS'),'D') + 7 ASA FROM DUAL;

A

-----------

2013/12/1

 

2           取月的開始時間和結束時間
  • 取月的開始時間

SQL>SELECT TRUNC(TO_DATE('2013-11-25 10:31:11','YYYY-MM-DD HH24:MI:SS'),'MM') AS AFROM DUAL;

A

-----------

2013/11/1

 

  • 取月的結束時間

SQL>SELECT LAST_DAY(TO_DATE('2013-11-25 10:31:11','YYYY-MM-DD HH24:MI:SS')) AS AFROM DUAL;

A

-----------

2013/11/30

 

3           取季的開始時間和結束時間
  • 取季的開始時間

SQL>SELECT TRUNC(TO_DATE('2013-11-25 10:31:11','YYYY-MM-DD HH24:MI:SS'),'Q') AS AFROM DUAL;

A

-----------

2013/10/1

 

  • 取季的結束時間

SQL>SELECT ADD_MONTHS(TRUNC(TO_DATE('2013-11-25 10:31:11','YYYY-MM-DDHH24:MI:SS'),'Q'),3)-1 AS A FROM DUAL;

A

-----------

2013/12/31

 

4           取年的開始時間和結束時間
  • 取年的開始時間

SQL>SELECT TRUNC(TO_DATE('2013-11-25 10:31:11','YYYY-MM-DD HH24:MI:SS'),'YYYY') ASA FROM DUAL;

A

-----------

2013/1/1

 

  • 取年的結束時間

SQL>SELECT ADD_MONTHS(TRUNC(TO_DATE('2013-11-25 10:31:11','YYYY-MM-DDHH24:MI:SS'),'YYYY'),12)-1 AS A FROM DUAL;

A

-----------

2013/12/31

相關文章

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.