%MONTHS (Number of Months)

%MONTHS(number)

%MONTHS converts a number into a duration that can be added to a date or timestamp value.

%MONTHS can only be the right-hand value in an addition or subtraction operation. The left-hand value must be a date or timestamp. The result is a date or timestamp value with the appropriate number of months added or subtracted. For a date, the resulting value is in *ISO format.

In most cases, the result of adding or subtracting a given number of months is obvious. For example, 2000-03-15 + %MONTHS(1) is 2000-04-15. If the addition or subtraction would produce a nonexistent date (for example, February 30), the last day of the month is used instead.

Adding or subtracting a number of months to the 29th, 30th, or 31st day of a month may not be reversible. For example, 2000-03-31 + %MONTHS(1) - %MONTHS(1) is 2000-03-30.

For more information, see Date Operations or Built-in Functions.

Figure 1. %MONTHS and %YEARS Example
 *..1....+....2....+....3....+....4....+....5....+....6....+....7...+....
 /FREE

     // Determine the date in 3 years
     newdate = date + %YEARS(3);

     // Determine the date in 6 months prior
     loandate = duedate - %MONTHS(6);

     // Construct a timestamp from a date and time
     duestamp = duedate + t'12.00.00';
 /END-FREE