Date/Time and Interval Functions and Operators

Elasticsearch SQL offers a wide range of facilities for performing date/time manipulations.

Intervals

A common requirement when dealing with date/time in general revolves around the notion of interval, a topic that is worth exploring in the context of Elasticsearch and Elasticsearch SQL.

Elasticsearch has comprehensive support for date math both inside index names and queries. Inside Elasticsearch SQL the former is supported as is by passing the expression in the table name, while the latter is supported through the standard SQL INTERVAL.

The table below shows the mapping between Elasticsearch and Elasticsearch SQL:

Elasticsearch

Elasticsearch SQL

Index/Table datetime math

<index-{now/M{YYYY.MM}}>

Query date/time math

1y

INTERVAL 1 YEAR

2M

INTERVAL 2 MONTH

3w

INTERVAL 21 DAY

4d

INTERVAL 4 DAY

5h

INTERVAL 5 HOUR

6m

INTERVAL 6 MINUTE

7s

INTERVAL 7 SECOND

INTERVAL allows either YEAR and MONTH to be mixed together or DAY, HOUR, MINUTE and SECOND.

Elasticsearch SQL accepts also the plural for each time unit (e.g. both YEAR and YEARS are valid).

Example of the possible combinations below:

Interval

Description

INTERVAL ‘1-2’ YEAR TO MONTH

1 year and 2 months

INTERVAL ‘3 4’ DAYS TO HOURS

3 days and 4 hours

INTERVAL ‘5 6:12’ DAYS TO MINUTES

5 days, 6 hours and 12 minutes

INTERVAL ‘3 4:56:01’ DAY TO SECOND

3 days, 4 hours, 56 minutes and 1 second

INTERVAL ‘2 3:45:01.23456789’ DAY TO SECOND

2 days, 3 hours, 45 minutes, 1 second and 234567890 nanoseconds

INTERVAL ‘123:45’ HOUR TO MINUTES

123 hours and 45 minutes

INTERVAL ‘65:43:21.0123’ HOUR TO SECONDS

65 hours, 43 minutes, 21 seconds and 12300000 nanoseconds

INTERVAL ‘45:01.23’ MINUTES TO SECONDS

45 minutes, 1 second and 230000000 nanoseconds

Operators

Basic arithmetic operators (+, -, *) support date/time parameters as indicated below:

  1. SELECT INTERVAL 1 DAY + INTERVAL 53 MINUTES AS result;
  2. result
  3. ---------------
  4. +1 00:53:00
  1. SELECT CAST('1969-05-13T12:34:56' AS DATETIME) + INTERVAL 49 YEARS AS result;
  2. result
  3. --------------------
  4. 2018-05-13T12:34:56Z
  1. SELECT - INTERVAL '49-1' YEAR TO MONTH result;
  2. result
  3. ---------------
  4. -49-1
  1. SELECT INTERVAL '1' DAY - INTERVAL '2' HOURS AS result;
  2. result
  3. ---------------
  4. +0 22:00:00
  1. SELECT CAST('2018-05-13T12:34:56' AS DATETIME) - INTERVAL '2-8' YEAR TO MONTH AS result;
  2. result
  3. --------------------
  4. 2015-09-13T12:34:56Z
  1. SELECT -2 * INTERVAL '3' YEARS AS result;
  2. result
  3. ---------------
  4. -6-0

Functions

Functions that target date/time.

CURRENT_DATE/CURDATE

Synopsis:

  1. CURRENT_DATE
  2. CURRENT_DATE()
  3. CURDATE()

Input: none

Output: date

Description: Returns the date (no time part) when the current query reached the server. It can be used both as a keyword: CURRENT_DATE or as a function with no arguments: CURRENT_DATE().

Unlike CURRENT_DATE, CURDATE() can only be used as a function with no arguments and not as a keyword.

This method always returns the same value for its every occurrence within the same query.

  1. SELECT CURRENT_DATE AS result;
  2. result
  3. ------------------------
  4. 2018-12-12
  1. SELECT CURRENT_DATE() AS result;
  2. result
  3. ------------------------
  4. 2018-12-12
  1. SELECT CURDATE() AS result;
  2. result
  3. ------------------------
  4. 2018-12-12

Typically, this function (as well as its twin TODAY()) function is used for relative date filtering:

  1. SELECT first_name FROM emp WHERE hire_date > TODAY() - INTERVAL 35 YEARS ORDER BY first_name ASC LIMIT 5;
  2. first_name
  3. ------------
  4. Alejandro
  5. Amabile
  6. Anneke
  7. Anoosh
  8. Arumugam

CURRENT_TIME/CURTIME

Synopsis:

  1. CURRENT_TIME
  2. CURRENT_TIME([precision])
  3. CURTIME

Input:

fractional digits; optional

Output: time

Description: Returns the time when the current query reached the server. As a function, CURRENT_TIME() accepts precision as an optional parameter for rounding the second fractional digits (nanoseconds). The default precision is 3, meaning a milliseconds precision current time will be returned.

This method always returns the same value for its every occurrence within the same query.

  1. SELECT CURRENT_TIME AS result;
  2. result
  3. ------------------------
  4. 12:31:27.237Z
  1. SELECT CURRENT_TIME() AS result;
  2. result
  3. ------------------------
  4. 12:31:27.237Z
  1. SELECT CURTIME() AS result;
  2. result
  3. ------------------------
  4. 12:31:27.237Z
  1. SELECT CURRENT_TIME(1) AS result;
  2. result
  3. ------------------------
  4. 12:31:27.2Z

Typically, this function is used for relative date/time filtering:

  1. SELECT first_name FROM emp WHERE CAST(hire_date AS TIME) > CURRENT_TIME() - INTERVAL 20 MINUTES ORDER BY first_name ASC LIMIT 5;
  2. first_name
  3. ---------------
  4. Alejandro
  5. Amabile
  6. Anneke
  7. Anoosh
  8. Arumugam

Currently, using a precision greater than 3 doesn’t make any difference to the output of the function as the maximum number of second fractional digits returned is 3 (milliseconds).

CURRENT_TIMESTAMP

Synopsis:

  1. CURRENT_TIMESTAMP
  2. CURRENT_TIMESTAMP([precision])

Input:

fractional digits; optional

Output: date/time

Description: Returns the date/time when the current query reached the server. As a function, CURRENT_TIMESTAMP() accepts precision as an optional parameter for rounding the second fractional digits (nanoseconds). The default precision is 3, meaning a milliseconds precision current date/time will be returned.

This method always returns the same value for its every occurrence within the same query.

  1. SELECT CURRENT_TIMESTAMP AS result;
  2. result
  3. ------------------------
  4. 2018-12-12T14:48:52.448Z
  1. SELECT CURRENT_TIMESTAMP() AS result;
  2. result
  3. ------------------------
  4. 2018-12-12T14:48:52.448Z
  1. SELECT CURRENT_TIMESTAMP(1) AS result;
  2. result
  3. ------------------------
  4. 2018-12-12T14:48:52.4Z

Typically, this function (as well as its twin NOW()) function is used for relative date/time filtering:

  1. SELECT first_name FROM emp WHERE hire_date > NOW() - INTERVAL 100 YEARS ORDER BY first_name ASC LIMIT 5;
  2. first_name
  3. ---------------
  4. Alejandro
  5. Amabile
  6. Anneke
  7. Anoosh
  8. Arumugam

Currently, using a precision greater than 3 doesn’t make any difference to the output of the function as the maximum number of second fractional digits returned is 3 (milliseconds).

DATE_ADD/DATEADD/TIMESTAMP_ADD/TIMESTAMPADD

Synopsis:

  1. DATE_ADD(
  2. string_exp,
  3. integer_exp,
  4. datetime_exp)

Input:

string expression denoting the date/time unit to add to the date/datetime

integer expression denoting how many times the above unit should be added to/from the date/datetime, if a negative value is used it results to a subtraction from the date/datetime

date/datetime expression

Output: datetime

Description: Add the given number of date/time units to a date/datetime. If the number of units is negative then it’s subtracted from the date/datetime. If any of the three arguments is null a null is returned.

If the second argument is a long there is possibility of truncation since an integer value will be extracted and used from that long.

Datetime units to add/subtract

unit

abbreviations

year

years, yy, yyyy

quarter

quarters, qq, q

month

months, mm, m

dayofyear

dy, y

day

days, dd, d

week

weeks, wk, ww

weekday

weekdays, dw

hour

hours, hh

minute

minutes, mi, n

second

seconds, ss, s

millisecond

milliseconds, ms

microsecond

microseconds, mcs

nanosecond

nanoseconds, ns

  1. SELECT DATE_ADD('years', 10, '2019-09-04T11:22:33.000Z'::datetime) AS "+10 years";
  2. +10 years
  3. ------------------------
  4. 2029-09-04T11:22:33.000Z
  1. SELECT DATE_ADD('week', 10, '2019-09-04T11:22:33.000Z'::datetime) AS "+10 weeks";
  2. +10 weeks
  3. ------------------------
  4. 2019-11-13T11:22:33.000Z
  1. SELECT DATE_ADD('seconds', -1234, '2019-09-04T11:22:33.000Z'::datetime) AS "-1234 seconds";
  2. -1234 seconds
  3. ------------------------
  4. 2019-09-04T11:01:59.000Z
  1. SELECT DATE_ADD('qq', -417, '2019-09-04'::date) AS "-417 quarters";
  2. -417 quarters
  3. ------------------------
  4. 1915-06-04T00:00:00.000Z
  1. SELECT DATE_ADD('minutes', 9235, '2019-09-04'::date) AS "+9235 minutes";
  2. +9235 minutes
  3. ------------------------
  4. 2019-09-10T09:55:00.000Z

DATE_DIFF/DATEDIFF/TIMESTAMP_DIFF/TIMESTAMPDIFF

Synopsis:

  1. DATE_DIFF(
  2. string_exp,
  3. datetime_exp,
  4. datetime_exp)

Input:

string expression denoting the date/time unit difference between the following two date/datetime expressions

start date/datetime expression

end date/datetime expression

Output: integer

Description: Subtract the second argument from the third argument and return their difference in multiples of the unit specified in the first argument. If the second argument (start) is greater than the third argument (end), then negative values are returned. If any of the three arguments is null, a null is returned.

Datetime difference units

unit

abbreviations

year

years, yy, yyyy

quarter

quarters, qq, q

month

months, mm, m

dayofyear

dy, y

day

days, dd, d

week

weeks, wk, ww

weekday

weekdays, dw

hour

hours, hh

minute

minutes, mi, n

second

seconds, ss, s

millisecond

milliseconds, ms

microsecond

microseconds, mcs

nanosecond

nanoseconds, ns

  1. SELECT DATE_DIFF('years', '2019-09-04T11:22:33.000Z'::datetime, '2032-09-04T22:33:11.000Z'::datetime) AS "diffInYears";
  2. diffInYears
  3. ------------------------
  4. 13
  1. SELECT DATE_DIFF('week', '2019-09-04T11:22:33.000Z'::datetime, '2016-12-08T22:33:11.000Z'::datetime) AS "diffInWeeks";
  2. diffInWeeks
  3. ------------------------
  4. -143
  1. SELECT DATE_DIFF('seconds', '2019-09-04T11:22:33.123Z'::datetime, '2019-07-12T22:33:11.321Z'::datetime) AS "diffInSeconds";
  2. diffInSeconds
  3. ------------------------
  4. -4625362
  1. SELECT DATE_DIFF('qq', '2019-09-04'::date, '2025-04-25'::date) AS "diffInQuarters";
  2. diffInQuarters
  3. ------------------------
  4. 23

For hour and minute, DATEDIFF doesn’t do any rounding, but instead first truncates the more detailed time fields on the 2 dates to zero and then calculates the subtraction.

  1. SELECT DATEDIFF('hours', '2019-11-10T12:10:00.000Z'::datetime, '2019-11-10T23:59:59.999Z'::datetime) AS "diffInHours";
  2. diffInHours
  3. ------------------------
  4. 11
  1. SELECT DATEDIFF('minute', '2019-11-10T12:10:00.000Z'::datetime, '2019-11-10T12:15:59.999Z'::datetime) AS "diffInMinutes";
  2. diffInMinutes
  3. ------------------------
  4. 5
  1. SELECT DATE_DIFF('minutes', '2019-09-04'::date, '2015-08-17T22:33:11.567Z'::datetime) AS "diffInMinutes";
  2. diffInMinutes
  3. ------------------------
  4. -2128407

DATETIME_FORMAT

Synopsis:

  1. DATETIME_FORMAT(
  2. date_exp/datetime_exp/time_exp,
  3. string_exp)

Input:

date/datetime/time expression

format pattern

Output: string

Description: Returns the date/datetime/time as a string using the format specified in the 2nd argument. The formatting pattern used is the one from java.time.format.DateTimeFormatter. If any of the two arguments is null or the pattern is an empty string null is returned.

If the 1st argument is of type time, then pattern specified by the 2nd argument cannot contain date related units (e.g. dd, MM, YYYY, etc.). If it contains such units an error is returned.

  1. SELECT DATETIME_FORMAT(CAST('2020-04-05' AS DATE), 'dd/MM/YYYY') AS "date";
  2. date
  3. ------------------
  4. 05/04/2020
  1. SELECT DATETIME_FORMAT(CAST('2020-04-05T11:22:33.987654' AS DATETIME), 'dd/MM/YYYY HH:mm:ss.SS') AS "datetime";
  2. datetime
  3. ------------------
  4. 05/04/2020 11:22:33.98
  1. SELECT DATETIME_FORMAT(CAST('11:22:33.987' AS TIME), 'HH mm ss.S') AS "time";
  2. time
  3. ------------------
  4. 11 22 33.9

DATETIME_PARSE

Synopsis:

  1. DATETIME_PARSE(
  2. string_exp,
  3. string_exp)

Input:

datetime expression as a string

parsing pattern

Output: datetime

Description: Returns a datetime by parsing the 1st argument using the format specified in the 2nd argument. The parsing format pattern used is the one from java.time.format.DateTimeFormatter. If any of the two arguments is null or an empty string null is returned.

If the parsing pattern contains only date or only time units (e.g. dd/MM/uuuu, HH:mm:ss, etc.) an error is returned as the function needs to return a value of datetime type which must contain both.

  1. SELECT DATETIME_PARSE('07/04/2020 10:20:30.123', 'dd/MM/uuuu HH:mm:ss.SSS') AS "datetime";
  2. datetime
  3. ------------------------
  4. 2020-04-07T10:20:30.123Z
  1. SELECT DATETIME_PARSE('10:20:30 07/04/2020 Europe/Berlin', 'HH:mm:ss dd/MM/uuuu VV') AS "datetime";
  2. datetime
  3. ------------------------
  4. 2020-04-07T08:20:30.000Z

If timezone is not specified in the datetime string expression and the parsing pattern, the resulting datetime will have the time zone specified by the user through the time_zone/timezone REST/driver parameters with no conversion applied.

  1. {
  2. "query" : "SELECT DATETIME_PARSE('10:20:30 07/04/2020', 'HH:mm:ss dd/MM/uuuu') AS \"datetime\"",
  3. "time_zone" : "Europe/Athens"
  4. }
  5. datetime
  6. -----------------------------
  7. 2020-04-07T10:20:30.000+03:00

TIME_PARSE

Synopsis:

  1. TIME_PARSE(
  2. string_exp,
  3. string_exp)

Input:

time expression as a string

parsing pattern

Output: time

Description: Returns a time by parsing the 1st argument using the format specified in the 2nd argument. The parsing format pattern used is the one from java.time.format.DateTimeFormatter. If any of the two arguments is null or an empty string null is returned.

If the parsing pattern contains only date units (e.g. dd/MM/uuuu) an error is returned as the function needs to return a value of time type which will contain only time.

  1. SELECT TIME_PARSE('10:20:30.123', 'HH:mm:ss.SSS') AS "time";
  2. time
  3. ---------------
  4. 10:20:30.123Z
  1. SELECT TIME_PARSE('10:20:30-01:00', 'HH:mm:ssXXX') AS "time";
  2. time
  3. ---------------
  4. 11:20:30.000Z

If timezone is not specified in the time string expression and the parsing pattern, the resulting time will have the offset of the time zone specified by the user through the time_zone/timezone REST/driver parameters at the Unix epoch date (1970-01-01) with no conversion applied.

  1. {
  2. "query" : "SELECT DATETIME_PARSE('10:20:30', 'HH:mm:ss') AS \"time\"",
  3. "time_zone" : "Europe/Athens"
  4. }
  5. time
  6. ------------------------------------
  7. 10:20:30.000+02:00

DATE_PART/DATEPART

Synopsis:

  1. DATE_PART(
  2. string_exp,
  3. datetime_exp)

Input:

string expression denoting the unit to extract from the date/datetime

date/datetime expression

Output: integer

Description: Extract the specified unit from a date/datetime. If any of the two arguments is null a null is returned. It’s similar to EXTRACT but with different names and aliases for the units and provides more options (e.g.: TZOFFSET).

Datetime units to extract

unit

abbreviations

year

years, yy, yyyy

quarter

quarters, qq, q

month

months, mm, m

dayofyear

dy, y

day

days, dd, d

week

weeks, wk, ww

weekday

weekdays, dw

hour

hours, hh

minute

minutes, mi, n

second

seconds, ss, s

millisecond

milliseconds, ms

microsecond

microseconds, mcs

nanosecond

nanoseconds, ns

tzoffset

tz

  1. SELECT DATE_PART('year', '2019-09-22T11:22:33.123Z'::datetime) AS "years";
  2. years
  3. ----------
  4. 2019
  1. SELECT DATE_PART('mi', '2019-09-04T11:22:33.123Z'::datetime) AS mins;
  2. mins
  3. -----------
  4. 22
  1. SELECT DATE_PART('quarters', CAST('2019-09-24' AS DATE)) AS quarter;
  2. quarter
  3. -------------
  4. 3
  1. SELECT DATE_PART('month', CAST('2019-09-24' AS DATE)) AS month;
  2. month
  3. -------------
  4. 9

For week and weekday the unit is extracted using the non-ISO calculation, which means that a given week is considered to start from Sunday, not Monday.

  1. SELECT DATE_PART('week', '2019-09-22T11:22:33.123Z'::datetime) AS week;
  2. week
  3. ----------
  4. 39

The tzoffset returns the total number of minutes (signed) that represent the time zone’s offset.

  1. SELECT DATE_PART('tzoffset', '2019-09-04T11:22:33.123+05:15'::datetime) AS tz_mins;
  2. tz_mins
  3. --------------
  4. 315
  1. SELECT DATE_PART('tzoffset', '2019-09-04T11:22:33.123-03:49'::datetime) AS tz_mins;
  2. tz_mins
  3. --------------
  4. -229

DATE_TRUNC/DATETRUNC

Synopsis:

  1. DATE_TRUNC(
  2. string_exp,
  3. datetime_exp/interval_exp)

Input:

string expression denoting the unit to which the date/datetime/interval should be truncated to

date/datetime/interval expression

Output: datetime/interval

Description: Truncate the date/datetime/interval to the specified unit by setting all fields that are less significant than the specified one to zero (or one, for day, day of week and month). If any of the two arguments is null a null is returned. If the first argument is week and the second argument is of interval type, an error is thrown since the interval data type doesn’t support a week time unit.

Datetime truncation units

unit

abbreviations

millennium

millennia

century

centuries

decade

decades

year

years, yy, yyyy

quarter

quarters, qq, q

month

months, mm, m

week

weeks, wk, ww

day

days, dd, d

hour

hours, hh

minute

minutes, mi, n

second

seconds, ss, s

millisecond

milliseconds, ms

microsecond

microseconds, mcs

nanosecond

nanoseconds, ns

  1. SELECT DATE_TRUNC('millennium', '2019-09-04T11:22:33.123Z'::datetime) AS millennium;
  2. millennium
  3. ------------------------
  4. 2000-01-01T00:00:00.000Z
  1. SELECT DATETRUNC('week', '2019-08-24T11:22:33.123Z'::datetime) AS week;
  2. week
  3. ------------------------
  4. 2019-08-19T00:00:00.000Z
  1. SELECT DATE_TRUNC('mi', '2019-09-04T11:22:33.123Z'::datetime) AS mins;
  2. mins
  3. ------------------------
  4. 2019-09-04T11:22:00.000Z
  1. SELECT DATE_TRUNC('decade', CAST('2019-09-04' AS DATE)) AS decades;
  2. decades
  3. ------------------------
  4. 2010-01-01T00:00:00.000Z
  1. SELECT DATETRUNC('quarters', CAST('2019-09-04' AS DATE)) AS quarter;
  2. quarter
  3. ------------------------
  4. 2019-07-01T00:00:00.000Z
  1. SELECT DATE_TRUNC('centuries', INTERVAL '199-5' YEAR TO MONTH) AS centuries;
  2. centuries
  3. ------------------
  4. +100-0
  1. SELECT DATE_TRUNC('hours', INTERVAL '17 22:13:12' DAY TO SECONDS) AS hour;
  2. hour
  3. ------------------
  4. +17 22:00:00
  1. SELECT DATE_TRUNC('days', INTERVAL '19 15:24:19' DAY TO SECONDS) AS day;
  2. day
  3. ------------------
  4. +19 00:00:00

DAY_OF_MONTH/DOM/DAY

Synopsis:

  1. DAY_OF_MONTH(datetime_exp)

Input:

date/datetime expression

Output: integer

Description: Extract the day of the month from a date/datetime.

  1. SELECT DAY_OF_MONTH(CAST('2018-02-19T10:23:27Z' AS TIMESTAMP)) AS day;
  2. day
  3. ---------------
  4. 19

DAY_OF_WEEK/DAYOFWEEK/DOW

Synopsis:

  1. DAY_OF_WEEK(datetime_exp)

Input:

date/datetime expression

Output: integer

Description: Extract the day of the week from a date/datetime. Sunday is 1, Monday is 2, etc.

  1. SELECT DAY_OF_WEEK(CAST('2018-02-19T10:23:27Z' AS TIMESTAMP)) AS day;
  2. day
  3. ---------------
  4. 2

DAY_OF_YEAR/DOY

Synopsis:

  1. DAY_OF_YEAR(datetime_exp)

Input:

date/datetime expression

Output: integer

Description: Extract the day of the year from a date/datetime.

  1. SELECT DAY_OF_YEAR(CAST('2018-02-19T10:23:27Z' AS TIMESTAMP)) AS day;
  2. day
  3. ---------------
  4. 50

DAY_NAME/DAYNAME

Synopsis:

  1. DAY_NAME(datetime_exp)

Input:

date/datetime expression

Output: string

Description: Extract the day of the week from a date/datetime in text format (Monday, Tuesday…​).

  1. SELECT DAY_NAME(CAST('2018-02-19T10:23:27Z' AS TIMESTAMP)) AS day;
  2. day
  3. ---------------
  4. Monday

HOUR_OF_DAY/HOUR

Synopsis:

  1. HOUR_OF_DAY(datetime_exp)

Input:

date/datetime expression

Output: integer

Description: Extract the hour of the day from a date/datetime.

  1. SELECT HOUR_OF_DAY(CAST('2018-02-19T10:23:27Z' AS TIMESTAMP)) AS hour;
  2. hour
  3. ---------------
  4. 10

ISO_DAY_OF_WEEK/ISODAYOFWEEK/ISODOW/IDOW

Synopsis:

  1. ISO_DAY_OF_WEEK(datetime_exp)

Input:

date/datetime expression

Output: integer

Description: Extract the day of the week from a date/datetime, following the ISO 8601 standard. Monday is 1, Tuesday is 2, etc.

  1. SELECT ISO_DAY_OF_WEEK(CAST('2018-02-19T10:23:27Z' AS TIMESTAMP)) AS day;
  2. day
  3. ---------------
  4. 1

ISO_WEEK_OF_YEAR/ISOWEEKOFYEAR/ISOWEEK/IWOY/IW

Synopsis:

  1. ISO_WEEK_OF_YEAR(datetime_exp)

Input:

date/datetime expression

Output: integer

Description: Extract the week of the year from a date/datetime, following ISO 8601 standard. The first week of a year is the first week with a majority (4 or more) of its days in January.

  1. SELECT ISO_WEEK_OF_YEAR(CAST('2018-02-19T10:23:27Z' AS TIMESTAMP)) AS week;
  2. week
  3. ---------------
  4. 8

MINUTE_OF_DAY

Synopsis:

  1. MINUTE_OF_DAY(datetime_exp)

Input:

date/datetime expression

Output: integer

Description: Extract the minute of the day from a date/datetime.

  1. SELECT MINUTE_OF_DAY(CAST('2018-02-19T10:23:27Z' AS TIMESTAMP)) AS minute;
  2. minute
  3. ---------------
  4. 623

MINUTE_OF_HOUR/MINUTE

Synopsis:

  1. MINUTE_OF_HOUR(datetime_exp)

Input:

date/datetime expression

Output: integer

Description: Extract the minute of the hour from a date/datetime.

  1. SELECT MINUTE_OF_HOUR(CAST('2018-02-19T10:23:27Z' AS TIMESTAMP)) AS minute;
  2. minute
  3. ---------------
  4. 23

MONTH_OF_YEAR/MONTH

Synopsis:

  1. MONTH(datetime_exp)

Input:

date/datetime expression

Output: integer

Description: Extract the month of the year from a date/datetime.

  1. SELECT MONTH_OF_YEAR(CAST('2018-02-19T10:23:27Z' AS TIMESTAMP)) AS month;
  2. month
  3. ---------------
  4. 2

MONTH_NAME/MONTHNAME

Synopsis:

  1. MONTH_NAME(datetime_exp)

Input:

date/datetime expression

Output: string

Description: Extract the month from a date/datetime in text format (January, February…​).

  1. SELECT MONTH_NAME(CAST('2018-02-19T10:23:27Z' AS TIMESTAMP)) AS month;
  2. month
  3. ---------------
  4. February

NOW

Synopsis:

  1. NOW()

Input: none

Output: datetime

Description: This function offers the same functionality as CURRENT_TIMESTAMP() function: returns the datetime when the current query reached the server. This method always returns the same value for its every occurrence within the same query.

  1. SELECT NOW() AS result;
  2. result
  3. ------------------------
  4. 2018-12-12T14:48:52.448Z

Typically, this function (as well as its twin CURRENT_TIMESTAMP()) function is used for relative date/time filtering:

  1. SELECT first_name FROM emp WHERE hire_date > NOW() - INTERVAL 100 YEARS ORDER BY first_name ASC LIMIT 5;
  2. first_name
  3. ---------------
  4. Alejandro
  5. Amabile
  6. Anneke
  7. Anoosh
  8. Arumugam

SECOND_OF_MINUTE/SECOND

Synopsis:

  1. SECOND_OF_MINUTE(datetime_exp)

Input:

date/datetime expression

Output: integer

Description: Extract the second of the minute from a date/datetime.

  1. SELECT SECOND_OF_MINUTE(CAST('2018-02-19T10:23:27Z' AS TIMESTAMP)) AS second;
  2. second
  3. ---------------
  4. 27

QUARTER

Synopsis:

  1. QUARTER(datetime_exp)

Input:

date/datetime expression

Output: integer

Description: Extract the year quarter the date/datetime falls in.

  1. SELECT QUARTER(CAST('2018-02-19T10:23:27Z' AS TIMESTAMP)) AS quarter;
  2. quarter
  3. ---------------
  4. 1

TODAY

Synopsis:

  1. TODAY()

Input: none

Output: date

Description: This function offers the same functionality as CURRENT_DATE() function: returns the date when the current query reached the server. This method always returns the same value for its every occurrence within the same query.

  1. SELECT TODAY() AS result;
  2. result
  3. ------------------------
  4. 2018-12-12

Typically, this function (as well as its twin CURRENT_TIMESTAMP()) function is used for relative date filtering:

  1. SELECT first_name FROM emp WHERE hire_date > TODAY() - INTERVAL 35 YEARS ORDER BY first_name ASC LIMIT 5;
  2. first_name
  3. ------------
  4. Alejandro
  5. Amabile
  6. Anneke
  7. Anoosh
  8. Arumugam

WEEK_OF_YEAR/WEEK

Synopsis:

  1. WEEK_OF_YEAR(datetime_exp)

Input:

date/datetime expression

Output: integer

Description: Extract the week of the year from a date/datetime.

  1. SELECT WEEK(CAST('1988-01-05T09:22:10Z' AS TIMESTAMP)) AS week, ISOWEEK(CAST('1988-01-05T09:22:10Z' AS TIMESTAMP)) AS isoweek;
  2. week | isoweek
  3. ---------------+---------------
  4. 2 |1

YEAR

Synopsis:

  1. YEAR(datetime_exp)

Input:

date/datetime expression

Output: integer

Description: Extract the year from a date/datetime.

  1. SELECT YEAR(CAST('2018-02-19T10:23:27Z' AS TIMESTAMP)) AS year;
  2. year
  3. ---------------
  4. 2018

EXTRACT

Synopsis:

  1. EXTRACT(
  2. datetime_function
  3. FROM datetime_exp)

Input:

date/time function name

date/datetime expression

Output: integer

Description: Extract fields from a date/datetime by specifying the name of a datetime function. The following

  1. SELECT EXTRACT(DAY_OF_YEAR FROM CAST('2018-02-19T10:23:27Z' AS TIMESTAMP)) AS day;
  2. day
  3. ---------------
  4. 50

is the equivalent to

  1. SELECT DAY_OF_YEAR(CAST('2018-02-19T10:23:27Z' AS TIMESTAMP)) AS day;
  2. day
  3. ---------------
  4. 50