Table 9.33 shows the available functions for date/time value processing, with details appearing in the following subsections. Table 9.32 illustrates the behaviors of the basic arithmetic operators (+
, *
, etc.). For formatting functions, refer to Section 9.8. You should be familiar with the background information on date/time data types from Section 8.5.
In addition, the usual comparison operators shown in Table 9.1 are available for the date/time types. Dates and timestamps (with or without time zone) are all comparable, while times (with or without time zone) and intervals can only be compared to other values of the same data type. When comparing a timestamp without time zone to a timestamp with time zone, the former value is assumed to be given in the time zone specified by the TimeZone configuration parameter, and is rotated to UTC for comparison to the latter value (which is already in UTC internally). Similarly, a date value is assumed to represent midnight in the TimeZone
zone when comparing it to a timestamp.
All the functions and operators described below that take time
or timestamp
inputs actually come in two variants: one that takes time with time zone
or timestamp with time zone
, and one that takes time without time zone
or timestamp without time zone
. For brevity, these variants are not shown separately. Also, the +
and *
operators come in commutative pairs (for example both date
+
integer
and integer
+
date
); we show only one of each such pair.
Table 9.32. Date/Time Operators
Operator Description Example(s) |
---|
Add a number of days to a date
|
Add an interval to a date
|
Add a time-of-day to a date
|
Add intervals
|
Add an interval to a timestamp
|
Add an interval to a time
|
Negate an interval
|
Subtract dates, producing the number of days elapsed
|
Subtract a number of days from a date
|
Subtract an interval from a date
|
Subtract times
|
Subtract an interval from a time
|
Subtract an interval from a timestamp
|
Subtract intervals
|
Subtract timestamps (converting 24-hour intervals into days, similarly to
|
Multiply an interval by a scalar
|
Divide an interval by a scalar
|
Table 9.33. Date/Time Functions
Function Description Example(s) |
---|
Subtract arguments, producing a “symbolic” result that uses years and months, rather than just days
|
Subtract argument from
|
Current date and time (changes during statement execution); see Section 9.9.5
|
Current date; see Section 9.9.5
|
Current time of day; see Section 9.9.5
|
Current time of day, with limited precision; see Section 9.9.5
|
Current date and time (start of current transaction); see Section 9.9.5
|
Current date and time (start of current transaction), with limited precision; see Section 9.9.5
|
Add an
|
Bin input into specified interval aligned with specified origin; see Section 9.9.3
|
Get timestamp subfield (equivalent to
|
Get interval subfield (equivalent to
|
Subtract an
|
Truncate to specified precision; see Section 9.9.2
|
Truncate to specified precision in the specified time zone; see Section 9.9.2
|
Truncate to specified precision; see Section 9.9.2
|
Get timestamp subfield; see Section 9.9.1
|
Get interval subfield; see Section 9.9.1
|
Test for finite date (not +/-infinity)
|
Test for finite timestamp (not +/-infinity)
|
Test for finite interval (currently always true)
|
Adjust interval, converting 30-day time periods to months
|
Adjust interval, converting 24-hour time periods to days
|
Adjust interval using
|
Current time of day; see Section 9.9.5
|
Current time of day, with limited precision; see Section 9.9.5
|
Current date and time (start of current transaction); see Section 9.9.5
|
Current date and time (start of current transaction), with limited precision; see Section 9.9.5
|
Create date from year, month and day fields (negative years signify BC)
|
Create interval from years, months, weeks, days, hours, minutes and seconds fields, each of which can default to zero
|
Create time from hour, minute and seconds fields
|
Create timestamp from year, month, day, hour, minute and seconds fields (negative years signify BC)
|
Create timestamp with time zone from year, month, day, hour, minute and seconds fields (negative years signify BC). If
|
Current date and time (start of current transaction); see Section 9.9.5
|
Current date and time (start of current statement); see Section 9.9.5
|
Current date and time (like
|
Current date and time (start of current transaction); see Section 9.9.5
|
Convert Unix epoch (seconds since 1970-01-01 00:00:00+00) to timestamp with time zone
|