# Detailed explanation of "Oracle" database "Week Count"

Source: Internet
Author: User
Tags what date

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

The--WW algorithm starts every January 1 for the first week, date+6 the end of each week.

-for example, 20050101 is the first day of the first week, and the last day of the first week is 20050101+6=20050107

--Formula first day of the week: Date + week * 7-7

--the last day of the week: Date + week * 7-1

You'll find out how the format will run away.

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

--Date calculation, calculate the first day of the nth week and the last day is what date. by Keynes 2005.04.29

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

The--WW algorithm starts every January 1 for the first week, date+6 the end of each week.

-for example, 20050101 is the first day of the first week, and the last day of the first week is 20050101+6=20050107

--Formula first day of the week: Date + week * 7-7

--the last day of the week: Date + week * 7-1

-If the WW format is the main, the 1th, 17 weeks start as follows

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

--Verify the following

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>

The--IW algorithm is a week from Monday to Sunday, and the first Monday of the year is the first week,

For example, 20050101 is Saturday, so the IW algorithm is 53 weeks before the year before, and 20050103 is the beginning of the first week.

--Formula first day of the week: Next_day (date) + Week * 7-7

--the last day of the week: Next_day (date) + Week * 7-1

-If the IW format is the main, the 1th and 17 weeks are as follows

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 ') + 7-7 as First_day,next_day (to_ Date (' 20050101 ', ' yyyymmdd '), ' MONDAY ') + * 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>

--Verify the following

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

Other:

--==, this is the first week of "this month."

SELECT to_char (sysdate, ' ww ')-To_char (TRUNC (sysdate, ' MM '), ' WW ') + 1 as "Weekofmon" from dual;

Or

SELECT to_char (sysdate, ' W ') as "Weekofmon" from dual;

--==, today is the first few weeks of "this year."

Select To_char (sysdate, ' WW ') from dual;

Or

Select To_char (sysdate, ' IW ') from dual;

Note:

The IW and WW formats mentioned above are explained in Doc as follows

IW = Week of year (1-52 or 1-53) based on the ISO standard

&NBSP;WW = Week of year (1-53) where Week 1 starts on the "the Year" and "continues to" the Seventh day of The year.

Related Keywords:
Related Article

### 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.