Date Functions

CURRENT_DATE

CURRENT_DATE([timezone]) Returns current date in the timezone time zone (default UTC). Return value is of date type.

SELECT CURRENT_DATE()
2019-08-19
SELECT CURRENT_DATE('America/Los_Angeles')
2019-08-19
SELECT TYPEOF(CURRENT_DATE())
'date'
SELECT CAST(CURRENT_DATE() AS string)
'2019-08-19'
SELECT FORMAT_DATE('%Y-%h-%d', CURRENT_DATE())
'2019-Aug-19'

Here are some examples of comparison and interval arithmetic operations on the date type:

SELECT CURRENT_DATE() > PARSE_DATE('%Y-%m-%d', '2019-01-01')
true
SELECT CURRENT_DATE() > CURRENT_DATE() - INTERVAL 1 DAY
true
SELECT
    CURRENT_DATE() AS today,
    (CURRENT_DATE() - INTERVAL 1 WEEK) AS last_week
+-------------+--------------+ | today | last_week | |-------------+--------------+ | 2019-08-19 | 2019-08-12T | +-------------+--------------+

DATE Constructor

DATE(year, month, day)

Constructs value of type date based on year, month, and day.

SELECT TYPEOF(DATE(2019,8,17))
'date'
SELECT DATE(2019,8,17)
2019-08-17
SELECT CAST(DATE(2019,8,17) AS string)
'2019-08-17'
SELECT FORMAT_DATE('%Y-%h-%d', DATE(2019,8,17))
'2019-Aug-17'

Here are some examples of comparison and interval arithmetic operations on the date type:

SELECT CURRENT_DATE() > DATE(2019,1,1)
true
SELECT CURRENT_DATE() > DATE(2019,8,17) + INTERVAL 1 DAY
true
SELECT
    DATE(2019,8,19) AS today,
    (DATE(2019,8,19) - INTERVAL 7 DAY) AS last_week
+---------------+--------------+ | today | last_week | |---------------+--------------+ | 2019-08-19 | 2019-08-12T | +---------------+--------------+
SELECT TYPEOF(DATE(CURRENT_DATETIME()))
'date'
SELECT DATE(CURRENT_DATETIME())
2019-08-19
SELECT CAST(DATE(CURRENT_DATETIME()) AS string)
'2019-08-19'
SELECT FORMAT_DATE('%Y-%h-%d', DATE(CURRENT_DATETIME()))
'2019-Aug-19'

Here are some examples of comparison and interval arithmetic operations on the date type:

SELECT DATE(CURRENT_DATETIME()) > DATE(2019,1,1)
true
SELECT DATE(CURRENT_DATETIME()) > DATE(2019,8,19) - INTERVAL 1 DAY
true
SELECT
    DATE(2019,8,19) AS today,
    DATE(2019,8,19) - INTERVAL 7 DAY AS last_week
+---------------+--------------+ | today | last_week | |---------------+--------------+ | 2019-08-19 | 2019-08-12T | +---------------+--------------+

CURRENT_DATE([timezone]) Returns current date in the timezone time zone (default UTC). Return value is of date type.

SELECT TYPEOF(DATE(CURRENT_TIMESTAMP()))
'date'
SELECT DATE(CURRENT_TIMESTAMP())
2019-08-19
SELECT DATE(CURRENT_TIMESTAMP(), 'Australia/Sydney')
2019-08-20
SELECT CAST(DATE(CURRENT_TIMESTAMP()) AS string)
'2019-08-19'
SELECT FORMAT_DATE('%Y-%h-%d', DATE(CURRENT_TIMESTAMP()))
'2019-Aug-19'

Here are some examples of comparison and interval arithmetic operations on the date type:

SELECT DATE(CURRENT_TIMESTAMP()) > DATE(2019,1,1)
true
SELECT DATE(CURRENT_TIMESTAMP()) > DATE(2019,8,19) - INTERVAL 1 DAY
true
SELECT
    DATE(2019,8,19) AS today,
    (DATE(CURRENT_TIMESTAMP()) - INTERVAL 7 DAY) AS last_week
+---------------+--------------+ | today | last_week | |---------------+--------------+ | 2019-08-19 | 2019-08-12T | +---------------+--------------+

DATE_PARSE

DATE_PARSE(string, format) Parses the date string (formatted using the given format) into a date value.

The format specification is the standard strptime with CCTZ extensions. This function behaves exactly like PARSE_DATE but accepts parameters in different order. It exists for compatibility with DATE_PARSE function in Presto.

FORMAT_DATE

FORMAT_DATE(format, date) Converts date to string formatted using the given format.

The format specification is the standard strftime with CCTZ extensions.

 SELECT FORMAT_DATE('%Y/%m/%d', DATE '2018-05-26')
'2018/05/26'
SELECT FORMAT_DATE('%Y-%h-%d', DATE '2019-8-19')
'2019-Aug-19'

FORMAT_ISO8601 date

FORMAT_ISO8601(date)

Converts a date value to string using the ISO 8601 extended format. Returns string formatted using YYYY-mm-dd as the format specifier for years between 0000 and 9999. For negative years (before 1 BCE = year 0), we use a leading - sign. For years greater than 9999, we use a leading + sign.

SELECT FORMAT_ISO8601(DATE '-0427-00-00')
'-428-11-30'
SELECT FORMAT_ISO8601(DATE '2019-01-01')
'2019-01-01'
SELECT FORMAT_ISO8601(DATE '10000-01-01')
'+10000-01-01'

PARSE_DATE

PARSE_DATE(format, string) Parses the date string (formatted using the given format) into a date value.

The format specification is the standard strptime with CCTZ extensions.

SELECT PARSE_DATE('%Y-%m-%d', '2019-8-19')
2019-08-19
SELECT TYPEOF(PARSE_DATE('%Y-%m-%d', '2019-8-19'))
'date'

Here are some examples of comparison and interval arithmetic operations on the date type:

SELECT DATE(2019,8,19) > PARSE_DATE('%Y/%m/%d', '2019/8/17')
true
SELECT DATE(2019,8,19) > PARSE_DATE('%Y/%m/%d', '2019/8/19') - INTERVAL 1 DAY
true
SELECT
    PARSE_DATE('%Y/%m/%d', '2019/8/19') AS today,
    (PARSE_DATE('%Y/%m/%d', '2019/8/19') - INTERVAL 7 DAY) AS last_week
+---------------+--------------+ | today | last_week | |---------------+--------------+ | 2019-08-19 | 2019-08-12T | +---------------+--------------+

PARSE_DATE_ISO8601

PARSE_DATE_ISO8601(string) Parses a date from an ISO 8601 string without a timezone.

SELECT PARSE_DATE_ISO8601('2019-8-17')
2019-08-17
SELECT TYPEOF(PARSE_DATE_ISO8601('2019-8-19'))
'date'

Here are some examples of comparison and interval arithmetic operations on the date type:

SELECT DATE(2019,8,19) > PARSE_DATE_ISO8601('2019-8-17')
true
SELECT DATE '2019-08-19' > PARSE_DATE_ISO8601('2019-08-19') - INTERVAL 1 DAY
true
SELECT
    PARSE_DATE_ISO8601('2019-08-19') AS today,
    (PARSE_DATE_ISO8601('2019-8-19') - INTERVAL 7 DAY) AS last_week
+---------------+--------------+ | today | last_week | |---------------+--------------+ | 2019-08-19 | 2019-08-12T | +---------------+--------------+

DATE_FROM_UNIX_DATE

DATE_FROM_UNIX_DATE(number) Returns a date from the given number, the number of days since January 1st, 1970.

DATE_FROM_UNIX_DATE(unix_date)

Returns a Date from the given unix_date, the number of days since January 1st, 1970.

SELECT DATE_FROM_UNIX_DATE(18125)
2019-08-17
SELECT TYPEOF(DATE_FROM_UNIX_DATE(18127))
'date'

Here are some examples of comparison and interval arithmetic operations on the date type:

SELECT DATE(2019,8,19) > DATE_FROM_UNIX_DATE(18125)
true
SELECT DATE '2019-08-19' > DATE_FROM_UNIX_DATE(18127) - INTERVAL 1 DAY
true
SELECT
    DATE_FROM_UNIX_DATE(18127) AS today,
    (DATE_FROM_UNIX_DATE(18127) - INTERVAL 7 DAY) AS last_week
+---------------+--------------+ | today | last_week | |---------------+--------------+ | 2019-08-19 | 2019-08-12T | +---------------+--------------+

UNIX_DATE

UNIX_DATE(date) Given a date, returns an integer representing the number of days since January 1st, 1970.

UNIX_DATE(date)

Given a Date, returns an integer representing the number of days since January 1st, 1970.

SELECT UNIX_DATE(DATE(2019,8,19))
18127
SELECT TYPEOF(UNIX_DATE(DATE(2019,8,19)))
'int'

EXTRACT date

EXTRACT(part FROM date)

Extracts a component specified by part from date.

Here are supported values for part for date along with example queries.

PartQueryResult
MICROSECONDSELECT EXTRACT(MICROSECOND FROM DATE '2018-05-26')0
MILLISECONDSELECT EXTRACT(MILLISECOND FROM DATE '2018-05-26')0
SECONDSELECT EXTRACT(SECOND FROM DATE '2018-05-26')0
MINUTESELECT EXTRACT(MINUTE FROM DATE '2018-05-26')0
HOURSELECT EXTRACT(HOUR FROM DATE '2018-05-26')0
DAYSELECT EXTRACT(DAY FROM DATE '2018-05-26')26
DAYOFWEEKSELECT EXTRACT(DAYOFWEEK FROM DATE '2018-05-26')7
DOWSELECT EXTRACT(DOW FROM DATE '2018-05-26')7
DAYOFYEARSELECT EXTRACT(DAYOFYEAR FROM DATE '2018-05-26')146
DOYSELECT EXTRACT(DOY FROM DATE '2018-05-26')146
MONTHSELECT EXTRACT(MONTH FROM DATE '2018-05-26')5
QUARTERSELECT EXTRACT(QUARTER FROM DATE '2018-05-26')2
YEARSELECT EXTRACT(YEAR FROM DATE '2018-05-26')2018
DATESELECT EXTRACT(DATE FROM DATE '2018-05-26')2018-05-26
TIMESELECT EXTRACT(TIME FROM DATE '2018-05-26')00:00:00.000
EPOCHSELECT EXTRACT(EPOCH FROM DATE '2018-05-26')1527292800
ISOWEEKSELECT EXTRACT(ISOWEEK FROM DATE '2018-05-26')21
ISOYEARSELECT EXTRACT(ISOYEAR FROM DATE '2018-05-26')2018

DATE_TRUNC (date)

DATE_TRUNC(precision, date)

DATE_TRUNC(precision, date) Truncates the provided date (assumed at timezone UTC) to the precision level provided. To use an alternate timezone use AT TIME ZONE.

The precision may be specified as a string (see examples below), a microsecond interval (INTERVAL x MICROSECOND, INTERVAL x DAY, etc), or a moth interval (INTERVAL x MONTH, INTERVAL x YEAR).

Here are supported values for precision (case-insensitive) along with example queries:

TypePrecisionQueryResult
DATEMILLENNIUMSELECT DATE_TRUNC('MILLENNIUM', DATE '1999-11-20')1000-01-01
DATECENTURYSELECT DATE_TRUNC('CENTURY', DATE '1999-11-20')1900-01-01
DATEDECADESELECT DATE_TRUNC('DECADE', DATE '1999-11-20')1990-01-01
DATEYEARSELECT DATE_TRUNC('YEAR', DATE '1999-11-20')1999-01-01
DATEQUARTERSELECT DATE_TRUNC('QUARTER', DATE '1999-11-20')1999-10-01
DATEMONTHSELECT DATE_TRUNC('MONTH', DATE '1999-11-20')1999-11-01
DATEWEEKSELECT DATE_TRUNC('WEEK', DATE '1999-11-20')1999-11-14
DATEDAYSELECT DATE_TRUNC('DAY', DATE '1999-11-20')1999-11-20
DATEmicrosecond_intervalSELECT DATE_TRUNC(INTERVAL 2 DAY, DATE '1999-11-20')1999-11-19
DATEmonth_intervalSELECT DATE_TRUNC(INTERVAL 2 MONTH, DATE '1999-11-20')1999-11-01

DATE_DIFF (date)

DATE_DIFF(precision, start_date, end_date) Computes the difference between start_date and end_date at the precision level provided. The difference is truncated to the nearest integer.

DATE_DIFF(precision, start_date, end_date)

Here are supported values for precision (case-insensitive) along with example queries:

Note: A MONTH is equal to 30 DAYs. A QUARTER is equal to 90 DAYs. A YEAR is equal to 365 DAYs.

TypePrecisionQueryResult
DATEMILLENNIUMSELECT DATE_DIFF('MILLENNIUM', DATE '1999-11-20', DATE '2999-11-20')1
DATECENTURYSELECT DATE_DIFF('CENTURY', DATE '1999-11-20', DATE '2999-11-20')10
DATEDECADESELECT DATE_DIFF('DECADE', DATE '1999-11-20', DATE '2999-11-20')100
DATEYEARSELECT DATE_DIFF('YEAR', DATE '1999-11-20', DATE '2999-11-20')1000
DATEQUARTERSELECT DATE_DIFF('QUARTER', DATE '1999-11-20', DATE '2000-11-20')4
DATEMONTHSELECT DATE_DIFF('MONTH', DATE '1999-11-20', DATE '2000-11-20')12
DATEWEEKSELECT DATE_DIFF('WEEK', DATE '1999-11-20', DATE '1999-12-20')4
DATEDAYSELECT DATE_DIFF('DAY', DATE '1999-11-20', DATE '1999-11-28')8