DB2 Expressions - Datetime operations and durations
Datetime values can be incremented, decremented, and subtracted. These operations can involve decimal numbers called durations.
A duration is a number representing an interval of time.
A duration can be of one of the following types:
The only arithmetic operations that can be performed on datetime values are addition and subtraction. If a datetime value is the operand of addition, the other operand must be a duration. The specific rules governing the use of the addition operator with datetime values follow.
The rules for the use of the subtraction operator on datetime values are not the same as those for addition because a datetime value cannot be subtracted from a duration, and because the operation of subtracting two datetime values is not the same as the operation of subtracting a duration from a datetime value. The specific rules governing the use of the subtraction operator with datetime values follow.
Dates can be subtracted, incremented, or decremented.
IF DAY(DATE2) <= DAY(DATE1) THEN DAY(RESULT) = DAY(DATE1) - DAY(DATE2).
IF DAY(DATE2) > DAY(DATE1) THEN DAY(RESULT) = N + DAY(DATE1) - DAY(DATE2) where N = the last day of MONTH(DATE2). MONTH(DATE2) is then incremented by 1.
IF MONTH(DATE2) <= MONTH(DATE1) THEN MONTH(RESULT) = MONTH(DATE1) - MONTH(DATE2).
IF MONTH(DATE2) > MONTH(DATE1) THEN MONTH(RESULT) = 12 + MONTH(DATE1) - MONTH(DATE2). YEAR(DATE2) is then incremented by 1.
YEAR(RESULT) = YEAR(DATE1) - YEAR(DATE2).
For example, the result of DATE('3/15/2000') - '12/31/1999' is 00000215. (or, a duration of 0 years, 2 months, and 15 days).
If a duration of years is added or subtracted, only the year portion of the date is affected. The month is unchanged, as is the day unless the result would be February 29 of a non-leap-year. In this case, the day is changed to 28, and a warning indicator in the SQLCA is set to indicate the adjustment.
Similarly, if a duration of months is added or subtracted, only months and, if necessary, years are affected. The day portion of the date is unchanged unless the result would be invalid (September 31, for example). In this case, the day is set to the last day of the month, and a warning indicator in the SQLCA is set to indicate the adjustment.
Adding or subtracting a duration of days will, of course, affect the day portion of the date, and potentially the month and year.
Date durations, whether positive or negative, may also be added to and subtracted from dates. As with labeled durations, the result is a valid date, and a warning indicator is set in the SQLCA whenever an end-of-month adjustment is necessary.
When a positive date duration is added to a date, or a negative date duration is subtracted from a date, the date is incremented by the specified number of years, months, and days, in that order. Thus, DATE1 + X, where X is a positive DECIMAL(8,0) number, is equivalent to the expression:
DATE1 + YEAR(X) YEARS + MONTH(X) MONTHS + DAY(X) DAYS.
When a positive date duration is subtracted from a date, or a negative date duration is added to a date, the date is decremented by the specified number of days, months, and years, in that order. Thus, DATE1 - X, where X is a positive DECIMAL(8,0) number, is equivalent to the expression:
DATE1 - DAY(X) DAYS - MONTH(X) MONTHS - YEAR(X) YEARS.
When adding durations to dates, adding one month to a given date gives the same date one month later unless that date does not exist in the later month. In that case, the date is set to that of the last day of the later month. For example, January 28 plus one month gives February 28; and one month added to January 29, 30, or 31 results in either February 28 or, for a leap year, February 29.
Note: If one or more months is added to a given date and then the same number of months is subtracted from the result, the final date is not necessarily the same as the original date.
Times can be subtracted, incremented, or decremented.
If TIME1 is greater than or equal to TIME2, TIME2 is subtracted from TIME1.
If TIME1 is less than TIME2, however, TIME1 is subtracted from TIME2, and the sign of the result is made negative. The following procedural description clarifies the steps involved in the operation result = TIME1 - TIME2.
IF SECOND(TIME2) <= SECOND(TIME1) THEN SECOND(RESULT) = SECOND(TIME1) - SECOND(TIME2).
IF SECOND(TIME2) > SECOND(TIME1) THEN SECOND(RESULT) = 60 + SECOND(TIME1) - SECOND(TIME2). MINUTE(TIME2) is then incremented by 1.
IF MINUTE(TIME2) <= MINUTE(TIME1) THEN MINUTE(RESULT) = MINUTE(TIME1) - MINUTE(TIME2).
IF MINUTE(TIME1) > MINUTE(TIME1) THEN MINUTE(RESULT) = 60 + MINUTE(TIME1) - MINUTE(TIME2). HOUR(TIME2) is then incremented by 1.
HOUR(RESULT) = HOUR(TIME1) - HOUR(TIME2).
For example, the result of TIME('11:02:26') - '00:32:56' is 102930. (a duration of 10 hours, 29 minutes, and 30 seconds).
Similarly, if a duration of minutes is added or subtracted, only minutes and, if necessary, hours are affected. The seconds portion of the time is unchanged.
Adding or subtracting a duration of seconds will, of course, affect the seconds portion of the time, and potentially the minutes and hours.
Time durations, whether positive or negative, also can be added to and subtracted from times. The result is a time that has been incremented or decremented by the specified number of hours, minutes, and seconds, in that order. TIME1 + X, where "X" is a DECIMAL(6,0) number, is equivalent to the expression:
TIME1 + HOUR(X) HOURS + MINUTE(X) MINUTES + SECOND(X) SECONDS
When subtracting a labeled duration of SECOND or SECONDS with a value that includes fractions of a second, the subtraction is performed as if the time value has up to 12 fractional second digits but the result is returned with the fractional seconds truncated.
Note: Although the time '24:00:00' is accepted as a valid time, it is never returned as the result of time addition or subtraction, even if the duration operand is zero (for example, time('24:00:00')±0 seconds = '00:00:00').
Timestamps can be subtracted, incremented, or decremented.
If TS1 is greater than or equal to TS2, TS2 is subtracted from TS1. If TS1 is less than TS2, however, TS1 is subtracted from TS2 and the sign of the result is made negative. The following procedural description clarifies the steps involved in the operation result = TS1 - TS2:
IF SECOND(TS2,s) <= SECOND(TS1,s) THEN SECOND(RESULT,s) = SECOND(TS1,s) - SECOND(TS2,s).
IF SECOND(TS2,s) > SECOND(TS1,s) THEN SECOND(RESULT,s) = 60 + SECOND(TS1,s) - SECOND(TS2,s). MINUTE(TS2) is then incremented by 1.
The minutes part of the timestamps are subtracted as specified in the rules for subtracting times.
IF HOUR(TS2) <= HOUR(TS1) THEN HOUR(RESULT) = HOUR(TS1) - HOUR(TS2).
IF HOUR(TS2) > HOUR(TS1) THEN HOUR(RESULT) = 24 + HOUR(TS1) - HOUR(TS2) and DAY(TS2) is incremented by 1.
The date part of the timestamps is subtracted as specified in the rules for subtracting dates.
TIMESTAMP1 - YEAR(X) YEARS - MONTH(X) MONTHS - DAY(X) DAYS - HOUR(X) HOURS - MINUTE(X) MINUTES - SECOND(X, s) SECONDS
When subtracting a duration with non-zero scale or a labeled duration of SECOND or SECONDS with a value that includes fractions of a second, the subtraction is performed as if the timestamp value has up to 12 fractional second digits. The resulting value is assigned to a timestamp value with the timestamp precision of the timestamp operand which could result in truncation of fractional second digits.