6.13. Date and Time Functions and Operators
Time Zone Conversion
Date and Time Functions
current_date -> date
Returns the current date as of the start of the query.
current_time -> time with time zone
Returns the current time as of the start of the query.
current_timestamp -> timestamp with time zone
Returns the current timestamp as of the start of the query.
current_timezone
() → varcharReturns the current time zone in the format defined by IANA(e.g.,
America/Los_Angeles
) or as fixed offset from UTC (e.g.,+08:35
)date
(x) → dateThis is an alias for
CAST(x AS date)
.fromiso8601_timestamp
(_string) → timestamp with time zoneParses the ISO 8601 formatted
string
into atimestamp with time zone
.fromiso8601_date
(_string) → dateParses the ISO 8601 formatted
string
into adate
.fromunixtime
(_unixtime) → timestampReturns the UNIX timestamp
unixtime
as a timestamp.fromunixtime
(_unixtime, string) → timestamp with time zoneReturns the UNIX timestamp
unixtime
as a timestamp with time zoneusingstring
for the time zone.fromunixtime
(_unixtime, hours, minutes) → timestamp with time zoneReturns the UNIX timestamp
unixtime
as a timestamp with time zoneusinghours
andminutes
for the time zone offset.localtime -> time
Returns the current time as of the start of the query.
localtimestamp -> timestamp
Returns the current timestamp as of the start of the query.
This is an alias for
current_timestamp
.toiso8601
(_x) → varcharFormats
x
as an ISO 8601 string.x
can be date, timestamp, ortimestamp with time zone.tomilliseconds
(_interval) → biginttounixtime
(_timestamp) → double- Returns
timestamp
as a UNIX timestamp.
Note
The following SQL-standard functions do not use parenthesis:
current_date
current_time
current_timestamp
localtime
localtimestamp
The date_trunc
function supports the following units:
Unit | Example Truncated Value |
---|---|
second | 2001-08-22 03:04:05.000 |
minute | 2001-08-22 03:04:00.000 |
hour | 2001-08-22 03:00:00.000 |
day | 2001-08-22 00:00:00.000 |
week | 2001-08-20 00:00:00.000 |
month | 2001-08-01 00:00:00.000 |
quarter | 2001-07-01 00:00:00.000 |
year | 2001-01-01 00:00:00.000 |
The above examples use the timestamp 2001-08-22 03:04:05.321
as the input.
datetrunc
(_unit, x) → [same as input]- Returns
x
truncated tounit
.
Interval Functions
The functions in this section support the following interval units:
Unit | Description |
---|---|
millisecond | Milliseconds |
second | Seconds |
minute | Minutes |
hour | Hours |
day | Days |
week | Weeks |
month | Months |
quarter | Quarters of a year |
year | Years |
dateadd
(_unit, value, timestamp) → [same as input]Adds an interval
value
of typeunit
totimestamp
.Subtraction can be performed by using a negative value.datediff
(_unit, timestamp1, timestamp2) → bigint- Returns expressed in terms of
unit
.
Duration Function
The parse_duration
function supports the following units:
parseduration
(_string) → interval- Parses
string
of formatvalue unit
into an interval, wherevalue
is fractional number ofunit
values:
- SELECT parse_duration('42.8ms'); -- 0 00:00:00.043
- SELECT parse_duration('3.81 d'); -- 3 19:26:24.000
- SELECT parse_duration('5m'); -- 0 00:05:00.000
The functions in this section use a format string that is compatible withthe MySQL date_parse
and str_to_date
functions. The following table,based on the MySQL manual, describes the format specifiers:
Specifier | Description |
---|---|
%a | Abbreviated weekday name (Sun .. Sat ) |
%b | Abbreviated month name (Jan .. Dec ) |
%c | Month, numeric (1 .. 12 ) [4] |
%D | Day of the month with English suffix (0th , 1st , 2nd , 3rd , …) |
%d | Day of the month, numeric (01 .. 31 ) |
%e | Day of the month, numeric (1 .. 31 ) [4] |
%f | Fraction of second (6 digits for printing: 000000 .. 999000 ; 1 - 9 digits for parsing: 0 .. 999999999 ) |
%H | Hour (00 .. 23 ) |
%h | Hour (01 .. 12 ) |
%I | Hour (01 .. 12 ) |
%i | Minutes, numeric (00 .. 59 ) |
%j | Day of year (001 .. 366 ) |
%k | Hour (0 .. 23 ) |
%l | Hour (1 .. 12 ) |
%M | Month name (January .. December ) |
%m | Month, numeric (01 .. 12 ) [4] |
%p | AM or PM |
%r | Time, 12-hour (hh:mm:ss followed by AM or PM ) |
%S | Seconds (00 .. 59 ) |
%s | Seconds (00 .. 59 ) |
%T | Time, 24-hour (hh:mm:ss ) |
%U | Week (00 .. 53 ), where Sunday is the first day of the week |
%u | Week (00 .. 53 ), where Monday is the first day of the week |
%V | Week (01 .. 53 ), where Sunday is the first day of the week; used with %X |
%v | Week (01 .. 53 ), where Monday is the first day of the week; used with %x |
%W | Weekday name (Sunday .. Saturday ) |
%w | Day of the week (0 .. 6 ), where Sunday is the first day of the week |
%X | Year for the week where Sunday is the first day of the week, numeric, four digits; used with %V |
%x | Year for the week, where Monday is the first day of the week, numeric, four digits; used with %v |
%Y | Year, numeric, four digits |
%y | Year, numeric (two digits) [2] |
%% | A literal character |
%x | x , for any x not listed above |
Timestamp is truncated to milliseconds. |
[3] | This specifier is not supported yet. Consider using (it uses 1-7 instead of 0-6 ). |
[4] | (1, , 3, ) This specifier does not support 0 as a month or day. |
Warning
The following specifiers are not currently supported: %D %U %u %V %w %X
dateformat
(_timestamp, format) → varcharFormats
timestamp
as a string usingformat
.dateparse
(_string, format) → timestamp- Parses
string
into a timestamp usingformat
.
Java Date Functions
The functions in this section use a format string that is compatible withJodaTime’s pattern format.
formatdatetime
(_timestamp, format) → varcharFormats
timestamp
as a string usingformat
.parsedatetime
(_string, format) → timestamp with time zone- Parses
string
into a timestamp with time zone usingformat
.
Extraction Function
The extract
function supports the following fields:
The types supported by the extract
function vary depending on thefield to be extracted. Most fields support all date and time types.
extract
(field FROM x) → bigint- Returns
field
fromx
.
Note
This SQL-standard function uses special syntax for specifying the arguments.
day
(x) → bigintdayof_month
(_x) → bigintThis is an alias for .
dayof_week
(_x) → bigintReturns the ISO day of the week from
x
.The value ranges from1
(Monday) to7
(Sunday).dayof_year
(_x) → bigintReturns the day of the year from
x
.The value ranges from1
to366
.This is an alias for
day_of_week()
.doy
(x) → bigintThis is an alias for .
hour
(x) → bigintReturns the hour of the day from
x
.The value ranges from0
to23
.millisecond
(x) → bigintReturns the millisecond of the second from
x
.minute
(x) → bigintReturns the minute of the hour from
x
.month
(x) → bigintReturns the month of the year from
x
.quarter
(x) → bigintReturns the quarter of the year from
x
.The value ranges from1
to4
.second
(x) → bigintReturns the second of the minute from
x
.timezonehour
(_timestamp) → bigintReturns the hour of the time zone offset from
timestamp
.timezoneminute
(_timestamp) → bigintReturns the minute of the time zone offset from
timestamp
.week
(x) → bigintReturns the ISO week of the year from
x
.The value ranges from1
to53
.weekof_year
(_x) → bigintThis is an alias for .
year
(x) → bigintReturns the year from
x
.yearof_week
(_x) → bigintReturns the year of the ISO week from
x
.- This is an alias for .