Detailed explanation of Oracle date week and calculation of start and end time of week

1 ORACLE mid-week related knowledge description

1.1 Date formatting functions

TO_CHAR(X [,FORMAT]) : Convert X to a string in FORMAT format. X is a date, FORMAT is a format string that specifies what format X is converted to, and FORMAT is related to weeks with W, WW, IW, D, and FMWW.

The meaning of W  is the week of the month. It is to return the week number according to the standard week customized by ORACLE.

IW is the ISO standard week, which means that the ISO standard week takes the week as the "main line". There can be up to 53 weeks per year, but each year must include at least 52 weeks; If there are more than or equal to 4 days between December 31 of the current year, it will be set as the 53rd week of the current year, otherwise the remaining days will be classified as the 1st week of the next year; if it is less than 52 weeks, Then it will be made up in the next year; every week is fixed from Monday as the first day of the week, and Sunday as the seventh day of the week; for example: in Oracle, January 01, 2012 still belongs to the IW week Do not Day 7 of Week 52 in 2011. This is used more.

WW is a standard week customized by ORACLE, which means that January 1 of each year is the first day of the first week of the current year (regardless of the day of the week on January 1 of the current year); for example: 2014, 01, 01 is Wednesday, defined in Oracle as the first day of the first week of WW 2014. Generally rarely used.

D is the day of the week that returns the current date. It is calculated from Sunday to Saturday, which is to be noted.

FMWW starts from January 1st of the year (regardless of the day of the week) to the Sunday of the year as the first week, and the second week is counted from the first week of the year (this is the same as the IW algorithm) the end of the year The week ends on December 31 of that year.

1.2 Date and time arithmetic functions

NEXT_DAY(X,Y) : Used to calculate the time of the first week Y after X time. Y is a string representing the full name of a day of the week (such as Monday, Tuesday, etc.) in the language of the current session, or it can be a numeric value.

TRUNC(X [,FORMAT]) : truncate the date, D, IW, WW, W, FMWW are related to the week in FORMAT.

The meaning of W  is the week of the month. It is a week number that is returned according to the standard week customized by ORACLE.

IW is the ISO standard week and returns the Monday of the week that is the current date.

WW is the standard week of ORACLE customization. Returns the Monday of the ORACLE custom standard week.

The meaning of D  is to return the first day of the current week. What is strange is that according to D, the first day of the current week is Sunday. This is where we should pay attention.

FMWW starts from January 1st of the year (regardless of the day of the week) to the Sunday of the year as the first week, and the second week is counted from the first week of the year (this is the same as the IW algorithm) the end of the year The week ends on December 31 of that year. The start time of the week is somewhat different from IW during the New Year's Eve. For example, when FMWW is used in 2012-01-01, the start time of the week is 2012-01-01, and when IW is used, the start time of the week is 2011/12 /26.

ROUND(X [,FORMAT]) : The rounding of the date in FORMAT has DAY associated with the week. Round up to the nearest Sunday as Monday-Wednesday and Thursday-Sunday.

2 Take the start time and end time of a week according to the given time

--取周的开始时间和结束时间  
SELECT TRUNC(TO_DATE('2014-07-18','YYYY-MM-DD'),'IW') AS STARTDATE FROM DUAL;--本周周一  
SELECT TRUNC(TO_DATE('2014-07-18','YYYY-MM-DD'),'IW') + 6 AS ENDDATE FROM DUAL;--本周周日  
  
SELECT TRUNC(TO_DATE('2014-07-18','YYYY-MM-DD'),'IW') - 7 AS STARTDATE FROM DUAL;---上周周一     
SELECT TRUNC(TO_DATE('2014-07-18','YYYY-MM-DD'),'IW') - 1  AS ENDDATE FROM DUAL;--上周周日  

 

 

3 Get the start time and end time of a week based on the given number of weeks

 

The difficulty of taking the start time and end time of a natural week is to determine whether the days at the beginning of the year belong to the first week of the current year or the last week of the previous year. According to the definition of IW natural week, less than or equal to 3 days is If this year belongs to the last week of the previous year, if more than or equal to 4 days belong to this year, add the last few days of the previous year to calculate the first week of the current year.

--按照周一到周日为一周算周的开始时间和结束时间(IW)自然周  
WITH PARAMS AS (SELECT TRUNC(TO_DATE('2009-01-01','YYYY-MM-DD'),'YYYY') AS SD FROM DUAL)  
SELECT LEVEL 周次,  
DECODE(SIGN(5-DECODE(TO_CHAR(PM.SD,'D'),'1','7',TO_CHAR(PM.SD,'D'))),-1,  
NEXT_DAY(PM.SD+(LEVEL-1)*7,2),NEXT_DAY(PM.SD+(LEVEL-1)*7-7,2))  
 当周第一天,  
DECODE(SIGN(5-DECODE(TO_CHAR(PM.SD,'D'),'1','7',TO_CHAR(PM.SD,'D'))),-1,  
NEXT_DAY(PM.SD+(LEVEL-1)*7,2),NEXT_DAY(PM.SD+(LEVEL-1)*7-7,2)) + 6  
当周最后一天  
FROM DUAL D  
LEFT JOIN PARAMS PM ON 1=1  
CONNECT BY LEVEL<=53  

--按照周日到周六为一周算周的开始时间和结束时间(D)  
SELECT LEVEL 周次,(TRUNC(TO_DATE('2011-01-01','YYYY-MM-DD'),'YYYY')-7) + (7-TO_CHAR(TRUNC(TO_DATE('2011-01-01','YYYY-MM-DD'),'YYYY'),'D')+1)+(LEVEL-1)*7  当周第一天,  
    (TRUNC(TO_DATE('2011-01-01','YYYY-MM-DD'),'YYYY')-7) + (7-TO_CHAR(TRUNC(TO_DATE('2011-01-01','YYYY-MM-DD'),'YYYY'),'D')+1)+(LEVEL-1)*7+6 当周最后一天  
FROM DUAL CONNECT BY LEVEL<=53  

--按照ORACLE标准(WW)  
SELECT LEVEL 周次,TO_DATE('2013-01-01','YYYY-MM-DD')+(LEVEL-1)*7  当周第一天,  
    TO_DATE('2013-01-01','YYYY-MM-DD')+(LEVEL-1)*7+  
    DECODE((TO_CHAR(TO_DATE('2013-12-31','YYYY-MM-DD'),'DDD')-(LEVEL-1)*7),1,0,2,1,6) 当周最后一天  
FROM DUAL CONNECT BY LEVEL<=53  

 

4 Get the maximum number of weeks in a year

--获取一年中的最大周次(IW)中国日历自然周  
WITH PARAMS AS (SELECT '2014' AS NF FROM DUAL)  
SELECT TO_CHAR(TO_DATE(PM.NF || '-12-28','YYYY-MM-DD'),'IYYYIW') FROM DUAL LEFT JOIN PARAMS PM ON 1=1 

 

The above SQL also determines which week the card belongs to on December 28 each year, and also determines how many natural weeks there are in a year.

 

5 Special attention should be paid to the place

 

When taking the week, it is better to bring the year, first because the week is relative to the first week of the year, and second because when using IW, the start and end days of the year may be different. If the same number of weeks is generated, it is not clear which week belongs to the first week of the year.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325024044&siteId=291194637