Datetime operations and durations

Datetime values can be incremented, decremented, and subtracted. These operations can involve decimal numbers called durations.

Durations

A duration is a number representing an interval of time.

labeled-duration
Read syntax diagramSkip visual syntax diagram function(expression)constantcolumn-nameglobal-variablehost-variable YEARYEARSMONTHMONTHSDAY DAYS1HOURHOURSMINUTEMINUTESSECOND SECONDS2MICROSECONDMICROSECONDS
Notes:
  • 1 DAYS is the default for non-decimal numeric arithmetic involving dates and timestamps.
  • 2 SECONDS is the default for non-decimal numeric arithmetic involving time.
A duration can be of one of the following types:
labeled duration
A specific unit of time as expressed by a number (which can be the result of an expression) followed by one of the seven duration keywords: YEARS, MONTHS, DAYS, HOURS, MINUTES, SECONDS, or MICROSECONDS. (The singular form of these keywords is also acceptable: YEAR, MONTH, DAY, HOUR, MINUTE, SECOND, and MICROSECOND.) The number specified is converted as if it were assigned to a DECIMAL(15,0) number, except for SECONDS which uses DECIMAL(27,12) to allow 0 to 12 digits of fractional seconds to be included. A labeled duration can only be used as an operand of an arithmetic operator in which the other operand is a value of data type DATE, TIME, or TIMESTAMP. Thus, the expression HIREDATE + 2 MONTHS + 14 DAYS is valid, whereas the expression HIREDATE + (2 MONTHS + 14 DAYS) is not. In both of these expressions, the labeled durations are 2 MONTHS and 14 DAYS.
date duration
A number of years, months, and days, expressed as a DECIMAL(8,0) number. To be properly interpreted, the number must have the format yyyymmdd., where yyyy represents the number of years, mm the number of months, and dd the number of days. (The period in the format indicates a DECIMAL data type.) The result of subtracting one date value from another, as in the expression HIREDATE - BRTHDATE, is a date duration.
time duration
A number of hours, minutes, and seconds, expressed as a DECIMAL(6,0) number. To be properly interpreted, the number must have the format hhmmss., where hh represents the number of hours, mm the number of minutes, and ss the number of seconds. (The period in the format indicates a DECIMAL data type.) The result of subtracting one time value from another is a time duration.
timestamp duration
A number of years, months, days, hours, minutes, seconds, and fractional seconds, expressed as a DECIMAL(14+s,s) number, where s is the number of digits of fractional seconds ranging from 0 to 12. To be properly interpreted, the number must have the format yyyymmddhhmmss.nnnnnnnnnnnn, where yyyy, mm, dd, hh, mm, ss, and nnnnnnnnnnnn represent, respectively, the number of years, months, days, hours, minutes, seconds, and fractional seconds. The result of subtracting one timestamp value from another is a timestamp duration, with scale that matches the maximum timestamp precision of the timestamp operands.

Datetime arithmetic in SQL

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.
  • If one operand is a date, the other operand must be a date duration or labeled duration of YEARS, MONTHS, or DAYS.
  • If one operand is a time, the other operand must be a time duration or a labeled duration of HOURS, MINUTES, or SECONDS.
  • If one operand is a timestamp, the other operand must be a duration. Any type of duration is valid.
  • Neither operand of the addition operator can be a parameter marker.
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.
  • If the first operand is a timestamp, the second operand must be a date, a timestamp, a string representation of a date, a string representation of a timestamp, or a duration. If the second operand is a string representation of a timestamp, it is implicitly converted to a timestamp with the same precision of the first operand.
  • If the second operand is a timestamp, the first operand must be a date, a timestamp, a string representation of a date, or a string representation of a timestamp. If the first operand is a string representation of a timestamp, it is implicitly converted to a timestamp with the same precision of the second operand.
  • If the first operand is a date, the second operand must be a date, a date duration, a string representation of a date, or a labeled duration of YEARS, MONTHS, or DAYS.
  • If the second operand is a date, the first operand must be a date, or a string representation of a date.
  • If the first operand is a time, the second operand must be a time, a time duration, a string representation of a time, or a labeled duration of HOURS, MINUTES, or SECONDS.
  • If the second operand is a time, the first operand must be a time, or string representation of a time.
  • Neither operand of the subtraction operator can be a parameter marker.

Date arithmetic

Dates can be subtracted, incremented, or decremented.

  • The result of subtracting one date (DATE2) from another (DATE1) is a date duration that specifies the number of years, months, and days between the two dates. The data type of the result is DECIMAL(8,0). If DATE1 is greater than or equal to DATE2, DATE2 is subtracted from DATE1. If DATE1 is less than DATE2, however, DATE1 is subtracted from DATE2, and the sign of the result is made negative. The following procedural description clarifies the steps involved in the operation result = DATE1 - DATE2.
       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).

  • The result of adding a duration to a date, or of subtracting a duration from a date, is itself a date. (For the purposes of this operation, a month denotes the equivalent of a calendar page. Adding months to a date, then, is like turning the pages of a calendar, starting with the page on which the date appears.) The result must fall between the dates January 1, 0001 and December 31, 9999 inclusive.

    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.

Time arithmetic

Times can be subtracted, incremented, or decremented.

  • The result of subtracting one time (TIME2) from another (TIME1) is a time duration that specifies the number of hours, minutes, and seconds between the two times. The data type of the result is DECIMAL(6,0).

    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).

  • The result of adding a duration to a time, or of subtracting a duration from a time, is itself a time. Any overflow or underflow of hours is discarded, thereby ensuring that the result is always a time. If a duration of hours is added or subtracted, only the hours portion of the time is affected. The minutes and seconds are unchanged.

    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').

Timestamp arithmetic

Timestamps can be subtracted, incremented, or decremented.

  • The result of subtracting one timestamp (TS2) from another (TS1) is a timestamp duration that specifies the number of years, months, days, hours, minutes, seconds, and fractional seconds between the two timestamps. The data type of the result is DECIMAL(14+s,s), where s is the maximum timestamp precision of TS1 and TS2.
    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.

  • The result of subtracting a date (D1) from a timestamp (TS1) is the same as subtracting TIMESTAMP(D1) from TS1. Similarly, the result of subtracting one timestamp (TS1) from a date (D2) is the same as subtracting TS1 from TIMESTAMP(D2).
  • The result of adding a duration to a timestamp, or of subtracting a duration from a timestamp is itself a timestamp.The precision of the result timestamp matches the precision of the timestamp operand. The date arithmetic portion is performed as previously defined, except that an overflow or underflow of hours is carried into the date part of the result, which must be within the range of valid dates. The time arithmetic portion is similar to time arithmetic except that it also considers the fractional seconds included in the duration. Thus, subtracting a duration, X, from a timestamp, TIMESTAMP1, where X is a DECIMAL(14+s,s) number, is equivalent to the expression:
       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.