How to: |
Given a date or timestamp and a component, DTRUNC returns the first date within the period specified by that component.
DTRUNC(date_or_timestamp, date_period, extend)
where:
Date or date-time
Is the date or timestamp of interest, which must provide a full component date.
Is the period whose starting or ending date you want to find. Can be one of the following:
By default, the first day of the week will be Sunday, but this can be changed using the WEEKFIRST parameter.
Optional. Is a number that indicates how many of the specified date components to include in the resulting date period.
Since all intervals have to be the same size, the extend argument is limited to the following values for the date period:
In the following request, DTRUNC returns the first date of the quarter given the start date of the employee:
DEFINE FILE WF_RETAIL QTRSTART/YYMD = DTRUNC(START_DATE, QUARTER); END TABLE FILE WF_RETAIL PRINT START_DATE QTRSTART AS 'Start,of Quarter' BY EMPLOYEE_NUMBER WHERE EMPLOYEE_NUMBER CONTAINS 'AH' ON TABLE SET PAGE NOPAGE END
The output is:
The following request returns the date that is the start of the week for the start date of certain employees:
DEFINE FILE WF_RETAIL DAY1/WT = DTRUNC(START_DATE, DAY); WKSTART/YYMD = DTRUNC(START_DATE, WEEK); DAY2/WT = DTRUNC(WKSTART, DAY); END TABLE FILE WF_RETAIL PRINT START_DATE DAY1 AS 'DOW 1' WKSTART AS 'Start,of Week' DAY2 AS 'DOW 2' BY EMPLOYEE_NUMBER WHERE START_DATE GT '20130101' WHERE EMPLOYEE_NUMBER CONTAINS 'AH' ON TABLE SET PAGE NOPAGE END
The output is:
Employee Start Start Number Date DOW 1 of Week DOW 2 -------- ----- ----- ------- ----- AH118 2013/01/15 TUE 2013/01/13 SUN AH2272 2013/01/17 THU 2013/01/13 SUN AH288 2013/11/11 MON 2013/11/10 SUN AH3520 2013/09/23 MON 2013/09/22 SUN AH3591 2013/09/22 SUN 2013/09/22 SUN AH5177 2013/07/21 SUN 2013/07/21 SUN
The following request returns the dates that correspond to the first day of the week and the last day of the week for the given date.
DEFINE FILE WF_RETAIL WEEKSTART/YYMD = DTRUNC(START_DATE, WEEK); WEEKEND/YYMD = DTRUNC(START_DATE, WEEK_END); END TABLE FILE WF_RETAIL PRINT START_DATE WEEKSTART AS 'Start,of Week' WEEKEND AS 'End,of Week' BY EMPLOYEE_NUMBER WHERE EMPLOYEE_NUMBER CONTAINS 'AH1' ON TABLE SET PAGE NOPAGE END
The output is shown in the following image.
In the following request, given the date of birth for each employee, the DTRUNC function uses the extend argument to return the start date of the decade in which they were born.
DEFINE FILE WF_RETAIL BIRTH_DECADE/YYMD = DTRUNC(DATE_OF_BIRTH, YEAR, 10); END TABLE FILE WF_RETAIL PRINT DATE_OF_BIRTH BIRTH_DECADE AS 'Start,of Decade' BY EMPLOYEE_NUMBER ON TABLE SET PAGE NOPAGE END
The output is shown in the following image.