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 an untyped 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 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.
  • If the first operand is a timestamp, the second operand must be a timestamp, a string representation of a timestamp, or a duration.
  • If the second operand is a timestamp, the first operand must be a timestamp or a string representation of a timestamp.
  • Neither operand of the subtraction operator can be an untyped parameter marker.