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

How to:

The DATEADD function adds a unit to or subtracts a unit from a full component date format. A unit is one of the following:

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.


Top of page

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

Tip: There is an alternative way to add to or subtract from the date. As long as any standard date is internally presented as a whole number of the least significant component units (that is, a number of days for full component dates, a number of months for YYM or MY format dates, and so on), you can add/subtract the desired number of these units directly, without DATEADD. Note that you must assign the date result to the same format date field, or the same field. For example, assuming YYM_DATE is a date field of format YYM, you can add 13 months to it and assign the result to the field NEW_YYM_DT, in the following statement:

NEW_YYM_DT/YYM = YYM_DATE + 13;

Otherwise, a non-full component date must be converted to a full component date before using DATEADD.


iWay Software