Name
timestampadd, timestampdiff — ODBC-standard datetime arithmetic by interval unit
Synopsis
datetime timestampadd(
|
in interval integer, |
| in n integer, | |
in dt datetime); |
integer timestampdiff(
|
in interval integer, |
| in dt1 datetime, | |
in dt2 datetime); |
Description
timestampadd and timestampdiff
implement the ODBC scalar functions TIMESTAMPADD and
TIMESTAMPDIFF. They take an interval-unit code as their
first argument (the integer corresponding to one of the standard ODBC
interval keywords: SQL_TSI_SECOND,
SQL_TSI_MINUTE, SQL_TSI_HOUR,
SQL_TSI_DAY, SQL_TSI_MONTH,
SQL_TSI_YEAR, etc.).
timestampadd returns dt with
n intervals of the chosen unit added. Negative
n subtracts. The result preserves the input's
timezone information. This is the procedural equivalent of
dateadd; prefer
dateadd for human-readable code.
timestampdiff returns the count of complete intervals
of the chosen unit between dt1 and
dt2 (positive when dt2 is
later). The day unit is computed directly from the date
parts; finer units require both inputs to have matching
timezone/timezone-less status, otherwise SQL state 22023
(DT014) is raised. Passing an unknown unit raises 22015
(DT004).