Shows the days of the week and the beginning of the week to the weekend

Source: Internet
Author: User
Tags datetime

Preface: Generally do data statistics write stored procedures when I believe that many friends have to deal with the date, which I was at that time to deal with the data, do statistical use. The date must be displayed when making the request. Especially on the week statistics, especially disgusting, so I used to date operation of the SQL in the stored procedure, to share with you. Hope to be able to help you. 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 few 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

Can be queried directly for the dates of Monday and Sunday of each month, and then the week

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

is to assign the data queried (select Rownum-1 rn from dual connect by rownum<=10000) to T 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 dates 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 dated from February 1, 2015 to February 1, 2015 plus 10,001 days between Monday

To run the displayed effect:


I want to write a stored procedure to do weekly and daily newspaper 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.