DB2 Version 9.7 for Linux, UNIX, and Windows

Datetime operations and durations

Datetime values can be incremented, decremented, and subtracted. These operations can involve decimal numbers called durations. The following sections describe duration types and detail the rules for datetime arithmetic.

Durations

A duration is a number representing an interval of time. There are four types of durations.

Read syntax diagramSkip visual syntax diagram
labeled-duration

|--+-function--------+--+-YEAR---------+------------------------|
   +-(expression)----+  +-YEARS--------+   
   +-constant--------+  +-MONTH--------+   
   +-column-name-----+  +-MONTHS-------+   
   +-global-variable-+  +-DAY----------+   
   '-host-variable---'  +-DAYS---------+   
                        +-HOUR---------+   
                        +-HOURS--------+   
                        +-MINUTE-------+   
                        +-MINUTES------+   
                        +-SECOND-------+   
                        +-SECONDS------+   
                        +-MICROSECOND--+   
                        '-MICROSECONDS-'   

A labeled duration represents 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.

A date duration represents 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.

A time duration represents 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.

A timestamp duration represents 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 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 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.

Time arithmetic

Times can be subtracted, incremented, or decremented.

Timestamp arithmetic

Timestamps can be subtracted, incremented, or decremented.