DATEADD: Adding or Subtracting a Date Unit to or From a Date

How to:

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:

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.

Syntax: How to Add or Subtract a Date Unit to or From a Date

DATEADD(date, 'component', increment)

where:

date

Date

Is a full component date.

component

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.

increment

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.

Note: DATEADD does not use an output argument. It uses the format of the date argument for the result. As long as the result is a full component date, it can be assigned only to a full component date field or to integer field.

Example: 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.

Example: 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.

Example: 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

Example: 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

Example: 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