Date/Time and Interval Functions and Operatorsedit

Warning

This functionality is in beta and is subject to change. The design and code is less mature than official GA features and is being provided as-is with no warranties. Beta features are not subject to the support SLA of official GA features.

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

Intervalsedit

A common requirement when dealing with date/time in general revolves around the notion of ``interval``s, 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 date math

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

Query date 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.

Tip

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

Operatorsedit

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

SELECT INTERVAL 1 DAY + INTERVAL 53 MINUTES AS result;

    result
---------------
+1 00:53:00.0
SELECT CAST('1969-05-13T12:34:56' AS DATE) + INTERVAL 49 YEARS AS result;

       result
--------------------
2018-05-13T12:34:56Z
SELECT - INTERVAL '49-1' YEAR TO MONTH result;

    result
---------------
-49-1
SELECT INTERVAL '1' DAY - INTERVAL '2' HOURS AS result;

    result
---------------
+0 22:00:00.0
SELECT CAST('2018-05-13T12:34:56' AS DATE) - INTERVAL '2-8' YEAR TO MONTH AS result;

       result
--------------------
2015-09-13T12:34:56Z
SELECT -2 * INTERVAL '3' YEARS AS result;

    result
---------------
-6-0

Functionsedit

Warning

This functionality is in beta and is subject to change. The design and code is less mature than official GA features and is being provided as-is with no warranties. Beta features are not subject to the support SLA of official GA features.

CURRENT_TIMESTAMP/NOWedit

Synopsis: 

CURRENT_TIMESTAMP
CURRENT_TIMESTAMP(precision )
NOW()

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).

This method always returns the same value within a query.

SELECT CURRENT_TIMESTAMP AS result;

         result
------------------------
2018-12-12T14:48:52.448Z
SELECT CURRENT_TIMESTAMP() AS result;

         result
------------------------
2018-12-12T14:48:52.448Z
SELECT CURRENT_TIMESTAMP(1) AS result;

         result
------------------------
2018-12-12T14:48:52.4Z

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

SELECT first_name FROM emp WHERE hire_date > NOW() - INTERVAL 100 YEARS ORDER BY first_name ASC LIMIT 5;

  first_name
---------------
Alejandro
Amabile
Anneke
Anoosh
Arumugam

DAY_OF_MONTH/DOM/DAYedit

Synopsis: 

DAY_OF_MONTH(date_exp)

Input:

date expression

Output: integer

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

SELECT DAY_OF_MONTH(CAST('2018-02-19T10:23:27Z' AS TIMESTAMP)) AS day;

      day
---------------
19

DAY_OF_WEEK/DAYOFWEEK/DOWedit

Synopsis: 

DAY_OF_WEEK(date_exp)

Input:

date expression

Output: integer

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

SELECT DAY_OF_WEEK(CAST('2018-02-19T10:23:27Z' AS TIMESTAMP)) AS day;

      day
---------------
2

DAY_OF_YEAR/DOYedit

Synopsis: 

DAY_OF_YEAR(date_exp)

Input:

date expression

Output: integer

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

SELECT DAY_OF_YEAR(CAST('2018-02-19T10:23:27Z' AS TIMESTAMP)) AS day;

      day
---------------
50

DAY_NAME/DAYNAMEedit

Synopsis: 

DAY_NAME(date_exp)

Input:

date expression

Output: string

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

SELECT DAY_NAME(CAST('2018-02-19T10:23:27Z' AS TIMESTAMP)) AS day;

      day
---------------
Monday

HOUR_OF_DAY/HOURedit

Synopsis: 

HOUR_OF_DAY(date_exp)

Input:

date expression

Output: integer

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

SELECT HOUR_OF_DAY(CAST('2018-02-19T10:23:27Z' AS TIMESTAMP)) AS hour;

     hour
---------------
10

ISO_DAY_OF_WEEK/ISODAYOFWEEK/ISODOW/IDOWedit

Synopsis: 

ISO_DAY_OF_WEEK(date_exp)

Input:

date expression

Output: integer

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

SELECT ISO_DAY_OF_WEEK(CAST('2018-02-19T10:23:27Z' AS TIMESTAMP)) AS day;

      day
---------------
1

ISO_WEEK_OF_YEAR/ISOWEEKOFYEAR/ISOWEEK/IWOY/IWedit

Synopsis: 

ISO_WEEK_OF_YEAR(date_exp)

Input:

date expression

Output: integer

Description: Extract the week of the year from a date, 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.

SELECT ISO_WEEK_OF_YEAR(CAST('2018-02-19T10:23:27Z' AS TIMESTAMP)) AS week;

     week
---------------
8

MINUTE_OF_DAYedit

Synopsis: 

MINUTE_OF_DAY(date_exp)

Input:

date expression

Output: integer

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

SELECT MINUTE_OF_DAY(CAST('2018-02-19T10:23:27Z' AS TIMESTAMP)) AS minute;

    minute
---------------
623

MINUTE_OF_HOUR/MINUTEedit

Synopsis: 

MINUTE_OF_HOUR(date_exp)

Input:

date expression

Output: integer

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

SELECT MINUTE_OF_HOUR(CAST('2018-02-19T10:23:27Z' AS TIMESTAMP)) AS minute;

    minute
---------------
23

MONTH_OF_YEAR/MONTHedit

Synopsis: 

MONTH(date_exp)

Input:

date expression

Output: integer

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

SELECT MONTH_OF_YEAR(CAST('2018-02-19T10:23:27Z' AS TIMESTAMP)) AS month;

     month
---------------
2

MONTH_NAME/MONTHNAMEedit

Synopsis: 

MONTH_NAME(date_exp)

Input:

date expression

Output: string

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

SELECT MONTH_NAME(CAST('2018-02-19T10:23:27Z' AS TIMESTAMP)) AS month;

     month
---------------
February

SECOND_OF_MINUTE/SECONDedit

Synopsis: 

SECOND_OF_MINUTE(date_exp)

Input:

date expression

Output: integer

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

SELECT SECOND_OF_MINUTE(CAST('2018-02-19T10:23:27Z' AS TIMESTAMP)) AS second;

    second
---------------
27

QUARTERedit

Synopsis: 

QUARTER(date_exp)

Input:

date expression

Output: integer

Description: Extract the year quarter the date falls in.

SELECT QUARTER(CAST('2018-02-19T10:23:27Z' AS TIMESTAMP)) AS quarter;

    quarter
---------------
1

WEEK_OF_YEAR/WEEKedit

Synopsis: 

WEEK_OF_YEAR(date_exp)

Input:

date expression

Output: integer

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

SELECT WEEK(CAST('1988-01-05T09:22:10Z' AS TIMESTAMP)) AS week, ISOWEEK(CAST('1988-01-05T09:22:10Z' AS TIMESTAMP)) AS isoweek;

      week     |   isoweek
---------------+---------------
2              |1

YEARedit

Synopsis: 

YEAR(date_exp)

Input:

date expression

Output: integer

Description: Extract the year from a date.

SELECT YEAR(CAST('2018-02-19T10:23:27Z' AS TIMESTAMP)) AS year;

     year
---------------
2018

EXTRACTedit

Synopsis: 

EXTRACT(datetime_function FROM date_exp)

Input:

datetime function name

date expression

Output: integer

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

SELECT EXTRACT(DAY_OF_YEAR FROM CAST('2018-02-19T10:23:27Z' AS TIMESTAMP)) AS day;

      day
---------------
50

is the equivalent to

SELECT DAY_OF_YEAR(CAST('2018-02-19T10:23:27Z' AS TIMESTAMP)) AS day;

      day
---------------
50