PostgreSQL: Date/Rime Functions and Operators
I kind of skipped over the functions/operators section of the PostgreSQL documentation when reading it. I want to review the sections that I think would be useful to me.
References
Notes
Date and timestamps 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 a timezone to a timestamp with a timezone, 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 UTC internally). Similarly, a date value is assumed to represent midnight in the TimeZone
zone when comparing it to a timestamp.
All functions and operators described that take time
or timestamp
inputs actually come in two variants: one with and one without the time zone.
In addition to the operators and functions seen in the tables below, the SQL OVERLAPS
operator is supported:
(start1, end1) OVERLAPS (start2, end2)
(start1, length1) OVERLAPS (start2, length2)
This expression yields true when two time periods (defined by their endpoints) overlap, false when they do not overlap. The endpoints can be specified as pairs of dates, times, or time stamps; or as a date, time, or time stamp followed by an interval. When a pair of values is provided, either the start or the end can be written first; OVERLAPS
automatically takes the earlier value of the pair as the start. Each time period is considered to represent the half-open interval start <= time < end
unless start
and end
are equal, in which case it represents that single time instance.
When adding an interval
value to (or subtracting an interval
value from) a timestamp with timzezone
value, the months, days, and microseconds of the interval
value are handled in turn.
EXTRACT(field from source)
The extract
function retrieves subfields such as year or hour from date/time values. source
must be a value expression of type timestamp
, date
, time
, or interval
. field
is an identifier or string that selects what field to extract from the source value.
Valid field names:
century
day
decade
dow
doy
epoch
hour
isodow
isoyear
julian
- ...
The function date_trunc
is conceptually similar to the trunc
function for numbers.
date_trunc(field , source [,time_zone]_
source
is a value expression of type timestamp [with time zone]
or interval
. field
selects to which precision to truncate the input value.
PostgreSQL provides a number of functions that return values related to the current date and time. These SQL-standard functions all return values based on the start time of the current transaction:
CURRENT_DATE
CURRENT_TIME
CURRENT_TIMESTAMP
CURRENT_TIME(precision)
CURRENT_TIMESTAMP(precision)
LOCALTIME
LOCALTIMESTAMP
LOCALTIME(precision)
LOCALTIMESTAMP(precision)
The following functions are available to delay execution of the server processes:
pg_sleep ( double precision )
pg_sleep_for ( interval )
pg_sleep_until ( timestamp with time zone )
Need to make sure that AI-generated SQL does not execute the above sleep functions.
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
|
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 (not +/-infinity)
|
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
|
Comments
You have to be logged in to add a comment
User Comments
There are currently no comments for this article.