CURRENT_DATE
CURRENT_DATE([timezone])
Returns current date in the timezone
time zone (default UTC). Return value is of date
type.
SELECT CURRENT_DATE()
SELECT CURRENT_DATE('America/Los_Angeles')
SELECT TYPEOF(CURRENT_DATE())
SELECT CAST(CURRENT_DATE() AS string)
SELECT FORMAT_DATE('%Y-%h-%d', CURRENT_DATE())
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')
SELECT CURRENT_DATE() > CURRENT_DATE() - INTERVAL 1 DAY
SELECT
CURRENT_DATE() AS today,
(CURRENT_DATE() - INTERVAL 1 WEEK) AS last_week
DATE Constructor
DATE(year, month, day)
Constructs value of type date
based on year
, month
, and day
.
SELECT TYPEOF(DATE(2019,8,17))
SELECT DATE(2019,8,17)
SELECT CAST(DATE(2019,8,17) AS string)
SELECT FORMAT_DATE('%Y-%h-%d', DATE(2019,8,17))
Here are some examples of comparison and interval arithmetic operations on the date
type:
SELECT CURRENT_DATE() > DATE(2019,1,1)
SELECT CURRENT_DATE() > DATE(2019,8,17) + INTERVAL 1 DAY
SELECT
DATE(2019,8,19) AS today,
(DATE(2019,8,19) - INTERVAL 7 DAY) AS last_week
SELECT TYPEOF(DATE(CURRENT_DATETIME()))
SELECT DATE(CURRENT_DATETIME())
SELECT CAST(DATE(CURRENT_DATETIME()) AS string)
SELECT FORMAT_DATE('%Y-%h-%d', DATE(CURRENT_DATETIME()))
Here are some examples of comparison and interval arithmetic operations on the date
type:
SELECT DATE(CURRENT_DATETIME()) > DATE(2019,1,1)
SELECT DATE(CURRENT_DATETIME()) > DATE(2019,8,19) - INTERVAL 1 DAY
SELECT
DATE(2019,8,19) AS today,
DATE(2019,8,19) - INTERVAL 7 DAY AS last_week
CURRENT_DATE([timezone])
Returns current date in the timezone
time zone (default UTC). Return value is of date
type.
SELECT TYPEOF(DATE(CURRENT_TIMESTAMP()))
SELECT DATE(CURRENT_TIMESTAMP())
SELECT DATE(CURRENT_TIMESTAMP(), 'Australia/Sydney')
SELECT CAST(DATE(CURRENT_TIMESTAMP()) AS string)
SELECT FORMAT_DATE('%Y-%h-%d', DATE(CURRENT_TIMESTAMP()))
Here are some examples of comparison and interval arithmetic operations on the date
type:
SELECT DATE(CURRENT_TIMESTAMP()) > DATE(2019,1,1)
SELECT DATE(CURRENT_TIMESTAMP()) > DATE(2019,8,19) - INTERVAL 1 DAY
SELECT
DATE(2019,8,19) AS today,
(DATE(CURRENT_TIMESTAMP()) - INTERVAL 7 DAY) AS last_week
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')
SELECT FORMAT_DATE('%Y-%h-%d', DATE '2019-8-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')
SELECT FORMAT_ISO8601(DATE '2019-01-01')
SELECT FORMAT_ISO8601(DATE '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')
SELECT TYPEOF(PARSE_DATE('%Y-%m-%d', '2019-8-19'))
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')
SELECT DATE(2019,8,19) > PARSE_DATE('%Y/%m/%d', '2019/8/19') - INTERVAL 1 DAY
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
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')
SELECT TYPEOF(PARSE_DATE_ISO8601('2019-8-19'))
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')
SELECT DATE '2019-08-19' > PARSE_DATE_ISO8601('2019-08-19') - INTERVAL 1 DAY
SELECT
PARSE_DATE_ISO8601('2019-08-19') AS today,
(PARSE_DATE_ISO8601('2019-8-19') - INTERVAL 7 DAY) AS last_week
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)
SELECT TYPEOF(DATE_FROM_UNIX_DATE(18127))
Here are some examples of comparison and interval arithmetic operations on the date
type:
SELECT DATE(2019,8,19) > DATE_FROM_UNIX_DATE(18125)
SELECT DATE '2019-08-19' > DATE_FROM_UNIX_DATE(18127) - INTERVAL 1 DAY
SELECT
DATE_FROM_UNIX_DATE(18127) AS today,
(DATE_FROM_UNIX_DATE(18127) - INTERVAL 7 DAY) AS last_week
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))
SELECT TYPEOF(UNIX_DATE(DATE(2019,8,19)))
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.
Part | Query | Result |
---|---|---|
MICROSECOND | SELECT EXTRACT(MICROSECOND FROM DATE '2018-05-26') | 0 |
MILLISECOND | SELECT EXTRACT(MILLISECOND FROM DATE '2018-05-26') | 0 |
SECOND | SELECT EXTRACT(SECOND FROM DATE '2018-05-26') | 0 |
MINUTE | SELECT EXTRACT(MINUTE FROM DATE '2018-05-26') | 0 |
HOUR | SELECT EXTRACT(HOUR FROM DATE '2018-05-26') | 0 |
DAY | SELECT EXTRACT(DAY FROM DATE '2018-05-26') | 26 |
DAYOFWEEK | SELECT EXTRACT(DAYOFWEEK FROM DATE '2018-05-26') | 7 |
DOW | SELECT EXTRACT(DOW FROM DATE '2018-05-26') | 7 |
DAYOFYEAR | SELECT EXTRACT(DAYOFYEAR FROM DATE '2018-05-26') | 146 |
DOY | SELECT EXTRACT(DOY FROM DATE '2018-05-26') | 146 |
MONTH | SELECT EXTRACT(MONTH FROM DATE '2018-05-26') | 5 |
QUARTER | SELECT EXTRACT(QUARTER FROM DATE '2018-05-26') | 2 |
YEAR | SELECT EXTRACT(YEAR FROM DATE '2018-05-26') | 2018 |
DATE | SELECT EXTRACT(DATE FROM DATE '2018-05-26') | 2018-05-26 |
TIME | SELECT EXTRACT(TIME FROM DATE '2018-05-26') | 00:00:00.000 |
EPOCH | SELECT EXTRACT(EPOCH FROM DATE '2018-05-26') | 1527292800 |
ISOWEEK | SELECT EXTRACT(ISOWEEK FROM DATE '2018-05-26') | 21 |
ISOYEAR | SELECT 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:
Type | Precision | Query | Result |
---|---|---|---|
DATE | MILLENNIUM | SELECT DATE_TRUNC('MILLENNIUM', DATE '1999-11-20') | 1000-01-01 |
DATE | CENTURY | SELECT DATE_TRUNC('CENTURY', DATE '1999-11-20') | 1900-01-01 |
DATE | DECADE | SELECT DATE_TRUNC('DECADE', DATE '1999-11-20') | 1990-01-01 |
DATE | YEAR | SELECT DATE_TRUNC('YEAR', DATE '1999-11-20') | 1999-01-01 |
DATE | QUARTER | SELECT DATE_TRUNC('QUARTER', DATE '1999-11-20') | 1999-10-01 |
DATE | MONTH | SELECT DATE_TRUNC('MONTH', DATE '1999-11-20') | 1999-11-01 |
DATE | WEEK | SELECT DATE_TRUNC('WEEK', DATE '1999-11-20') | 1999-11-14 |
DATE | DAY | SELECT DATE_TRUNC('DAY', DATE '1999-11-20') | 1999-11-20 |
DATE | microsecond_interval | SELECT DATE_TRUNC(INTERVAL 2 DAY, DATE '1999-11-20') | 1999-11-19 |
DATE | month_interval | SELECT 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 DAY
s. A QUARTER
is equal to 90 DAY
s. A YEAR
is equal to 365 DAY
s.
Type | Precision | Query | Result |
---|---|---|---|
DATE | MILLENNIUM | SELECT DATE_DIFF('MILLENNIUM', DATE '1999-11-20', DATE '2999-11-20') | 1 |
DATE | CENTURY | SELECT DATE_DIFF('CENTURY', DATE '1999-11-20', DATE '2999-11-20') | 10 |
DATE | DECADE | SELECT DATE_DIFF('DECADE', DATE '1999-11-20', DATE '2999-11-20') | 100 |
DATE | YEAR | SELECT DATE_DIFF('YEAR', DATE '1999-11-20', DATE '2999-11-20') | 1000 |
DATE | QUARTER | SELECT DATE_DIFF('QUARTER', DATE '1999-11-20', DATE '2000-11-20') | 4 |
DATE | MONTH | SELECT DATE_DIFF('MONTH', DATE '1999-11-20', DATE '2000-11-20') | 12 |
DATE | WEEK | SELECT DATE_DIFF('WEEK', DATE '1999-11-20', DATE '1999-12-20') | 4 |
DATE | DAY | SELECT DATE_DIFF('DAY', DATE '1999-11-20', DATE '1999-11-28') | 8 |