DATEADD: Adding or Subtracting a Date Unit to or From a Date
Available Languages: reporting, Maintain
The DATEADD function adds a unit to or subtracts a unit from a full component date format. A unit is one of the following:
- Year.
- Month. If the calculation using the month unit creates an invalid date, DATEADD corrects it to the last day of the month. For example, adding one month to October 31 yields November 30, not November 31, since November has 30 days.
- Day.
- Weekday. When using the weekday unit, DATEADD does not count Saturday or Sunday. For example, if you add one day to Friday, first DATEADD moves to the next weekday, Monday, then it adds a day. The result is Tuesday.
- Business day. When using the business day unit, DATEADD uses the BUSDAYS parameter setting and holiday file to determine which days are working days and disregards the rest. If Monday is not a working day, then one business day past Sunday is Tuesday. See Using Standard Date Functions for more information.
Note that when the DATEADD function calculates the next or previous business day or work day, it always starts from a business day or work day. So if the actual day is Saturday or Sunday, and the request wants to calculate the next business day, the function will use Monday as the starting day, not Saturday or Sunday, and will return Tuesday as the next business day. Similarly, when calculating the previous business day, it will use the starting day Friday, and will return Thursday as the previous business day. You can use the DATEMOV function to move the date to the correct type of day before using DATEADD. For more information, see DATEMOV: Moving a Date to a Significant Point.
DATEADD requires a date to be in date format. Since Dialogue Manager interprets a date as alphanumeric or numeric, and DATEADD requires a standard date stored as an offset from the base date, do not use DATEADD with Dialogue Manager unless you first convert the variable used as the input date to an offset from the base date.
For more information, see Calling a Function From a Dialogue Manager Command.
You add or subtract non day-based dates (for example, YM or YQ) directly without using DATEADD.
DATEADD works only with full component dates.
Add or Subtract a Date Unit to or From a Date
DATEADD(date, 'component', increment)
where:
Date
Is a full component date.
Alphanumeric
Is one of the following enclosed in single quotation marks:
Y
indicates
a year component.
M
indicates
a month component.
D
indicates
a day component.
WD
indicates
a weekday component.
BD
indicates a business
day component.
Integer
Is the number of date units added to or subtracted from date. If this number is not a whole unit, it is rounded down to the next largest integer.
Adding or Subtracting a Date Unit to or From a Date
This example finds a delivery date that is 12 business days after today:
DELIV_DATE/YYMD = DATEADD('&DATEMDYY', 'BD', 12);
It returns 20040408, which will be Thursday if today is March 23 2004, Tuesday.
To make sure it is Thursday, assign it as
DELIV_DAY/W = DATEADD('&DATEMDYY', 'BD', 12);
which returns 4, representing Thursday. Note the use of the system variable &YYMD and the natural date representation of the today's date.
NEW_YYM_DT/YYM = YYM_DATE + 13;
Otherwise, a non-full component date must be converted to a full component date before using DATEADD.
Truncation With DATEADD
The number of units passed to DATEADD is always a whole unit. For example
DATEADD(DATE, 'M', 1.999)
adds one month because the number of units is less than two.
Using the Weekday Unit
If you use the weekday unit and a Saturday or Sunday is the input date, DATEADD changes the input date to Monday. The function
DATEADD('910623', 'WD', 1)
in which DATE is either Saturday or Sunday yields Tuesday; Saturday and Sunday are not weekdays, so DATEADD begins with Monday and adds one.
Note that the single quotes around the number in the first argument, ‘910623’, causes it to be treated as a natural date literal.
Adding Weekdays to a Date (Reporting)
DATEADD adds three weekdays to NEW_DATE. In some cases, it adds more than three days because HIRE_DATE_PLUS_THREE would otherwise be on a weekend.
TABLE FILE EMPLOYEE PRINT FIRST_NAME AND HIRE_DATE AND COMPUTE NEW_DATE/YYMD = HIRE_DATE; HIRE_DATE_PLUS_THREE/YYMD = DATEADD(NEW_DATE, 'WD', 3); BY LAST_NAME WHERE DEPARTMENT EQ 'MIS'; END
The output is:
LAST_NAME FIRST_NAME HIRE_DATE NEW_DATE HIRE_DATE_PLUS_THREE --------- ---------- --------- -------- -------------------- BLACKWOOD ROSEMARIE 82/04/01 1982/04/01 1982/04/06 CROSS BARBARA 81/11/02 1981/11/02 1981/11/05 GREENSPAN MARY 82/04/01 1982/04/01 1982/04/06 JONES DIANE 82/05/01 1982/05/01 1982/05/06 MCCOY JOHN 81/07/01 1981/07/01 1981/07/06 SMITH MARY 81/07/01 1981/07/01 1981/07/06
Determining If a Date Is a Work Day (Reporting)
DATEADD determines which values in the TRANSDATE field do not represent work days by adding zero days to TRANSDATE using the business day unit. If TRANSDATE does not represent a business day, DATEADD returns the next business day to DATEX. TRANSDATE is then compared to DATEX, and the day of the week is printed for all dates that do not match between the two fields, resulting in a list of all non-work days.
DEFINE FILE VIDEOTRK DATEX/YMD = DATEADD(TRANSDATE, 'BD', 0); DATEINT/I8YYMD = DATECVT(TRANSDATE, 'YMD','I8YYMD'); END TABLE FILE VIDEOTRK SUM TRANSDATE NOPRINT COMPUTE DAYNAME/A8 = DOWKL(DATEINT, DAYNAME); AS 'Day of Week' BY TRANSDATE AS 'Date' WHERE TRANSDATE NE DATEX END
The output is:
Date Day of Week ---- ----------- 91/06/22 SATURDAY 91/06/23 SUNDAY 91/06/30 SUNDAY
Adding Months to a Date (Maintain)
DATEADD adds months to the DATE1 field:
MAINTAIN compute DATE1/yymd = '20000101' compute DATE2/yymd=dateadd(date1, 'M', 2, date2); type "DATE1 = <<DATE1 + 2 MONTHS = DATE2 = <<DATE2" END
The result is:
DATE1 = 2000/01/01+ 2 MONTHS = DATE2 = 2000/03/01