Date and time operators
Time zone conversion
TheAT TIME ZONE operator sets the time zone of a timestamp:
Date and time functions
current_date
current_time
current_timestamp
3 digits of subsecond precision,
p digits of subsecond precision:
current_timezone
America/Los_Angeles) or as fixed offset from UTC (e.g., +08:35)
date
CAST(x AS date).
last_day_of_month
from_iso8601_timestamp
string, optionally with time and time
zone, into a timestamp(3) with time zone. The time defaults to
00:00:00.000, and the time zone defaults to the session time zone:
from_iso8601_timestamp_nanos
string. The time zone
defaults to the session time zone:
from_iso8601_date
string into a date. The date can
be a calendar date, a week date using ISO week numbering, or year and day
of year combined:
at_timezone
timestamp with the time zone
converted from the session time zone to the time zone specified in zone
with precision p. In the following example, the session time zone is set
to America/New_York, which is three hours ahead of
America/Los_Angeles:
with_timezone
timestamp with the time zone
specified in zone with precision p:
from_unixtime
unixtime as a timestamp with time zone. unixtime is the
number of seconds since 1970-01-01 00:00:00 UTC.
unixtime as a timestamp with time zone
using zone for the time zone. unixtime is the number of seconds
since 1970-01-01 00:00:00 UTC.
unixtime as a timestamp with time zone
using hours and minutes for the time zone offset. unixtime is
the number of seconds since 1970-01-01 00:00:00 in double data type.
from_unixtime_nanos
unixtime as a timestamp with time zone. unixtime is the
number of nanoseconds since 1970-01-01 00:00:00.000000000 UTC:
localtime
localtimestamp
3
digits of subsecond precision.
p digits of subsecond precision:
now
current_timestamp.
to_iso8601
x as an ISO 8601 string. x can be date, timestamp, or
timestamp with time zone.
to_milliseconds
interval as milliseconds.
to_unixtime
timestamp as a UNIX timestamp.
The following SQL-standard functions do not use parenthesis:
current_datecurrent_timecurrent_timestamplocaltimelocaltimestamp
Truncation function
Thedate_trunc function supports the following units:
The above examples use the timestamp
2001-08-22 03:04:05.321 as the input.
date_trunc
x truncated to unit:
Interval functions
The functions in this section support the following interval units:date_add
value of type unit to timestamp.
Subtraction can be performed by using a negative value:
date_diff
timestamp2 - timestamp1 expressed in terms of unit:
Duration function
Theparse_duration function supports the following units:
parse_duration
string of format value unit into an interval, where
value is fractional number of unit values:
human_readable_seconds
seconds into a human readable string containing
weeks, days, hours, minutes, and seconds:
MySQL date functions
The functions in this section use a format string that is compatible with the MySQLdate_parse and str_to_date functions. The following table,
based on the MySQL manual, describes the format specifiers:
date_format
timestamp as a string using format:
date_parse
string into a timestamp using format:
Java date functions
The functions in this section use a format string that is compatible with JodaTime’s DateTimeFormat pattern format.format_datetime
timestamp as a string using format.
parse_datetime
string into a timestamp with time zone using format.
Extraction function
Theextract function supports the following fields:
The types supported by the
extract function vary depending on the
field to be extracted. Most fields support all date and time types.
extract
field from x:
This SQL-standard function uses special syntax for specifying the arguments.
Convenience extraction functions
day
x.
day_of_month
day.
day_of_week
x.
The value ranges from 1 (Monday) to 7 (Sunday).
day_of_year
x.
The value ranges from 1 to 366.
dow
day_of_week.
doy
day_of_year.
hour
x.
The value ranges from 0 to 23.
millisecond
x.
minute
x.
month
x.
quarter
x.
The value ranges from 1 to 4.
second
x.
timezone_hour
timestamp.
timezone_minute
timestamp.
week
x.
The value ranges from 1 to 53.
week_of_year
year
x.
year_of_week
x.