Shows the number of weeks per month and the date of the week beginning to the weekend

Source: Internet
Author: User
Tags datetime

Preface: Generally do data statistics write stored procedures when I believe many friends have to deal with the date, this is my data processing, do statistics. At that time the request must show a date. Especially when the statistics of the week, especially disgusting, so I used to date the operation of the stored procedures in the SQL, to share with you. Hope to be able to be helpful to everybody. Not much nonsense, directly on the SQL.

Select A.* from (
Select To_char (datetime, ' yyyy-mm ') as month, datetime as start date, datetime+6 as end date,
To_char (dateTime, ' W ')
As the first weeks from
(
Select Datetime,rownum rn from
(
With T as
(select Rownum-1 rn from dual connect by rownum<=10000)
Select To_date (' 2015-02-01 ', ' yyyy-mm-dd ') +rn as dateTime from T where To_date (' 2015-02-01 ', ' yyyy-mm-dd ') +rn<= Sysdate
b where To_char (B.datetime, ' day ') = ' Monday '
)
) A

You can directly query for the date of Monday and Sunday of each month, and then belong to the first few weeks

With T as
(select Rownum-1 rn from dual connect by rownum<=10000)

is to assign to t the data queried (select Rownum-1 rn from dual connect by rownum<=10000) for easy use

With T as
(select Rownum-1 rn from dual connect by rownum<=10000)
Select To_date (' 2015-02-01 ', ' yyyy-mm-dd ') +rn as dateTime from T where To_date (' 2015-02-01 ', ' yyyy-mm-dd ') +rn<= Sysdate

Get a date from February 1, 2015 to February 1, 2015 plus 10,001 days

Select Datetime,rownum rn from
(
With T as
(select Rownum-1 rn from dual connect by rownum<=10000)
Select To_date (' 2015-02-01 ', ' yyyy-mm-dd ') +rn as dateTime from T where To_date (' 2015-02-01 ', ' yyyy-mm-dd ') +rn<= Sysdate
b where To_char (B.datetime, ' day ') = ' Monday '

This SQL can be obtained from February 1, 2015 to February 1, 2015 plus 10,001 days between Monday dates

To run the effect of the display:


I would like to do weekly and daily work on the write stored procedures a little help.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.