123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708 |
- [role="xpack"]
- [testenv="basic"]
- [[sql-functions-datetime]]
- === Date/Time and Interval Functions and Operators
- {es-sql} offers a wide range of facilities for performing date/time manipulations.
- [[sql-functions-datetime-interval]]
- ==== 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 {es} and {es-sql}.
- {es} has comprehensive support for <<date-math, date math>> both inside <<date-math-index-names, index names>> and <<mapping-date-format, queries>>.
- Inside {es-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 {es} and {es-sql}:
- [cols="^m,^m"]
- |==========================
- s|{es}
- s|{es-sql}
- 2+h| Index/Table datetime math
- 2+|<index-{now/M{YYYY.MM}}>
- 2+h| 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`.
- TIP: {es-sql} accepts also the plural for each time unit (e.g. both `YEAR` and `YEARS` are valid).
- Example of the possible combinations below:
- [cols="^,^"]
- |===
- s|Interval
- s|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 (`+`, `-`, etc) support date/time parameters as indicated below:
- ["source","sql",subs="attributes,callouts,macros"]
- --------------------------------------------------
- include-tagged::{sql-specs}/docs/docs.csv-spec[dtIntervalPlusInterval]
- --------------------------------------------------
- ["source","sql",subs="attributes,callouts,macros"]
- --------------------------------------------------
- include-tagged::{sql-specs}/docs/docs.csv-spec[dtDateTimePlusInterval]
- --------------------------------------------------
- ["source","sql",subs="attributes,callouts,macros"]
- --------------------------------------------------
- include-tagged::{sql-specs}/docs/docs.csv-spec[dtMinusInterval]
- --------------------------------------------------
- ["source","sql",subs="attributes,callouts,macros"]
- --------------------------------------------------
- include-tagged::{sql-specs}/docs/docs.csv-spec[dtIntervalMinusInterval]
- --------------------------------------------------
- ["source","sql",subs="attributes,callouts,macros"]
- --------------------------------------------------
- include-tagged::{sql-specs}/docs/docs.csv-spec[dtDateTimeMinusInterval]
- --------------------------------------------------
- ["source","sql",subs="attributes,callouts,macros"]
- --------------------------------------------------
- include-tagged::{sql-specs}/docs/docs.csv-spec[dtIntervalMul]
- --------------------------------------------------
- ==== Functions
- Functions that target date/time.
- [[sql-functions-current-date]]
- ==== `CURRENT_DATE/CURDATE`
- .Synopsis:
- [source, sql]
- --------------------------------------------------
- CURRENT_DATE
- CURRENT_DATE()
- 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()`.
- [NOTE]
- 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.
- ["source","sql",subs="attributes,callouts,macros"]
- --------------------------------------------------
- include-tagged::{sql-specs}/docs/docs.csv-spec[currentDate]
- --------------------------------------------------
- ["source","sql",subs="attributes,callouts,macros"]
- --------------------------------------------------
- include-tagged::{sql-specs}/docs/docs.csv-spec[currentDateFunction]
- --------------------------------------------------
- ["source","sql",subs="attributes,callouts,macros"]
- --------------------------------------------------
- include-tagged::{sql-specs}/docs/docs.csv-spec[curDateFunction]
- --------------------------------------------------
- Typically, this function (as well as its twin <<sql-functions-today,TODAY())>> function
- is used for relative date filtering:
- ["source","sql",subs="attributes,callouts,macros"]
- --------------------------------------------------
- include-tagged::{sql-specs}/docs/docs.csv-spec[filterToday]
- --------------------------------------------------
- [[sql-functions-current-time]]
- ==== `CURRENT_TIME/CURTIME`
- .Synopsis:
- [source, sql]
- --------------------------------------------------
- CURRENT_TIME
- CURRENT_TIME([precision <1>])
- CURTIME
- --------------------------------------------------
- *Input*:
- <1> 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.
- ["source","sql",subs="attributes,callouts,macros"]
- --------------------------------------------------
- include-tagged::{sql-specs}/docs/docs.csv-spec[currentTime]
- --------------------------------------------------
- ["source","sql",subs="attributes,callouts,macros"]
- --------------------------------------------------
- include-tagged::{sql-specs}/docs/docs.csv-spec[currentTimeFunction]
- --------------------------------------------------
- ["source","sql",subs="attributes,callouts,macros"]
- --------------------------------------------------
- include-tagged::{sql-specs}/docs/docs.csv-spec[curTimeFunction]
- --------------------------------------------------
- ["source","sql",subs="attributes,callouts,macros"]
- --------------------------------------------------
- include-tagged::{sql-specs}/docs/docs.csv-spec[currentTimeFunctionPrecision]
- --------------------------------------------------
- Typically, this function is used for relative date/time filtering:
- ["source","sql",subs="attributes,callouts,macros"]
- --------------------------------------------------
- include-tagged::{sql-specs}/docs/docs.csv-spec[filterCurrentTime]
- --------------------------------------------------
- [IMPORTANT]
- 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).
- [[sql-functions-current-timestamp]]
- ==== `CURRENT_TIMESTAMP`
- .Synopsis:
- [source, sql]
- --------------------------------------------------
- CURRENT_TIMESTAMP
- CURRENT_TIMESTAMP(precision <1>)
- --------------------------------------------------
- *Input*:
- <1> 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.
- ["source","sql",subs="attributes,callouts,macros"]
- --------------------------------------------------
- include-tagged::{sql-specs}/docs/docs.csv-spec[curTs]
- --------------------------------------------------
- ["source","sql",subs="attributes,callouts,macros"]
- --------------------------------------------------
- include-tagged::{sql-specs}/docs/docs.csv-spec[curTsFunction]
- --------------------------------------------------
- ["source","sql",subs="attributes,callouts,macros"]
- --------------------------------------------------
- include-tagged::{sql-specs}/docs/docs.csv-spec[curTsFunctionPrecision]
- --------------------------------------------------
- Typically, this function (as well as its twin <<sql-functions-now,NOW())>> function is used for
- relative date/time filtering:
- ["source","sql",subs="attributes,callouts,macros"]
- --------------------------------------------------
- include-tagged::{sql-specs}/docs/docs.csv-spec[filterNow]
- --------------------------------------------------
- [IMPORTANT]
- 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).
- [[sql-functions-datetime-day]]
- ==== `DAY_OF_MONTH/DOM/DAY`
- .Synopsis:
- [source, sql]
- --------------------------------------------------
- DAY_OF_MONTH(datetime_exp<1>)
- --------------------------------------------------
- *Input*:
- <1> date/datetime expression
- *Output*: integer
- .Description:
- Extract the day of the month from a date/datetime.
- ["source","sql",subs="attributes,callouts,macros"]
- --------------------------------------------------
- include-tagged::{sql-specs}/docs/docs.csv-spec[dayOfMonth]
- --------------------------------------------------
- [[sql-functions-datetime-dow]]
- ==== `DAY_OF_WEEK/DAYOFWEEK/DOW`
- .Synopsis:
- [source, sql]
- --------------------------------------------------
- DAY_OF_WEEK(datetime_exp<1>)
- --------------------------------------------------
- *Input*:
- <1> date/datetime expression
- *Output*: integer
- .Description:
- Extract the day of the week from a date/datetime. Sunday is `1`, Monday is `2`, etc.
- ["source","sql",subs="attributes,callouts,macros"]
- --------------------------------------------------
- include-tagged::{sql-specs}/docs/docs.csv-spec[dayOfWeek]
- --------------------------------------------------
- [[sql-functions-datetime-doy]]
- ==== `DAY_OF_YEAR/DOY`
- .Synopsis:
- [source, sql]
- --------------------------------------------------
- DAY_OF_YEAR(datetime_exp<1>)
- --------------------------------------------------
- *Input*:
- <1> date/datetime expression
- *Output*: integer
- .Description:
- Extract the day of the year from a date/datetime.
- ["source","sql",subs="attributes,callouts,macros"]
- --------------------------------------------------
- include-tagged::{sql-specs}/docs/docs.csv-spec[dayOfYear]
- --------------------------------------------------
- [[sql-functions-datetime-dayname]]
- ==== `DAY_NAME/DAYNAME`
- .Synopsis:
- [source, sql]
- --------------------------------------------------
- DAY_NAME(datetime_exp<1>)
- --------------------------------------------------
- *Input*:
- <1> date/datetime expression
- *Output*: string
- .Description:
- Extract the day of the week from a date/datetime in text format (`Monday`, `Tuesday`...).
- ["source","sql",subs="attributes,callouts,macros"]
- --------------------------------------------------
- include-tagged::{sql-specs}/docs/docs.csv-spec[dayName]
- --------------------------------------------------
- [[sql-functions-datetime-hour]]
- ==== `HOUR_OF_DAY/HOUR`
- .Synopsis:
- [source, sql]
- --------------------------------------------------
- HOUR_OF_DAY(datetime_exp<1>)
- --------------------------------------------------
- *Input*:
- <1> date/datetime expression
- *Output*: integer
- .Description:
- Extract the hour of the day from a date/datetime.
- ["source","sql",subs="attributes,callouts,macros"]
- --------------------------------------------------
- include-tagged::{sql-specs}/docs/docs.csv-spec[hourOfDay]
- --------------------------------------------------
- [[sql-functions-datetime-isodow]]
- ==== `ISO_DAY_OF_WEEK/ISODAYOFWEEK/ISODOW/IDOW`
- .Synopsis:
- [source, sql]
- --------------------------------------------------
- ISO_DAY_OF_WEEK(datetime_exp<1>)
- --------------------------------------------------
- *Input*:
- <1> date/datetime expression
- *Output*: integer
- .Description:
- Extract the day of the week from a date/datetime, following the https://en.wikipedia.org/wiki/ISO_week_date[ISO 8601 standard].
- Monday is `1`, Tuesday is `2`, etc.
- ["source","sql",subs="attributes,callouts,macros"]
- --------------------------------------------------
- include-tagged::{sql-specs}/docs/docs.csv-spec[isoDayOfWeek]
- --------------------------------------------------
- [[sql-functions-datetime-isoweek]]
- ==== `ISO_WEEK_OF_YEAR/ISOWEEKOFYEAR/ISOWEEK/IWOY/IW`
- .Synopsis:
- [source, sql]
- --------------------------------------------------
- ISO_WEEK_OF_YEAR(datetime_exp<1>)
- --------------------------------------------------
- *Input*:
- <1> date/datetime expression
- *Output*: integer
- .Description:
- Extract the week of the year from a date/datetime, following https://en.wikipedia.org/wiki/ISO_week_date[ISO 8601 standard]. The first week
- of a year is the first week with a majority (4 or more) of its days in January.
- ["source","sql",subs="attributes,callouts,macros"]
- --------------------------------------------------
- include-tagged::{sql-specs}/docs/docs.csv-spec[isoWeekOfYear]
- --------------------------------------------------
- [[sql-functions-datetime-minuteofday]]
- ==== `MINUTE_OF_DAY`
- .Synopsis:
- [source, sql]
- --------------------------------------------------
- MINUTE_OF_DAY(datetime_exp<1>)
- --------------------------------------------------
- *Input*:
- <1> date/datetime expression
- *Output*: integer
- .Description:
- Extract the minute of the day from a date/datetime.
- ["source","sql",subs="attributes,callouts,macros"]
- --------------------------------------------------
- include-tagged::{sql-specs}/docs/docs.csv-spec[minuteOfDay]
- --------------------------------------------------
- [[sql-functions-datetime-minute]]
- ==== `MINUTE_OF_HOUR/MINUTE`
- .Synopsis:
- [source, sql]
- --------------------------------------------------
- MINUTE_OF_HOUR(datetime_exp<1>)
- --------------------------------------------------
- *Input*:
- <1> date/datetime expression
- *Output*: integer
- .Description:
- Extract the minute of the hour from a date/datetime.
- ["source","sql",subs="attributes,callouts,macros"]
- --------------------------------------------------
- include-tagged::{sql-specs}/docs/docs.csv-spec[minuteOfHour]
- --------------------------------------------------
- [[sql-functions-datetime-month]]
- ==== `MONTH_OF_YEAR/MONTH`
- .Synopsis:
- [source, sql]
- --------------------------------------------------
- MONTH(datetime_exp<1>)
- --------------------------------------------------
- *Input*:
- <1> date/datetime expression
- *Output*: integer
- .Description:
- Extract the month of the year from a date/datetime.
- ["source","sql",subs="attributes,callouts,macros"]
- --------------------------------------------------
- include-tagged::{sql-specs}/docs/docs.csv-spec[monthOfYear]
- --------------------------------------------------
- [[sql-functions-datetime-monthname]]
- ==== `MONTH_NAME/MONTHNAME`
- .Synopsis:
- [source, sql]
- --------------------------------------------------
- MONTH_NAME(datetime_exp<1>)
- --------------------------------------------------
- *Input*:
- <1> date/datetime expression
- *Output*: string
- .Description:
- Extract the month from a date/datetime in text format (`January`, `February`...).
- ["source","sql",subs="attributes,callouts,macros"]
- --------------------------------------------------
- include-tagged::{sql-specs}/docs/docs.csv-spec[monthName]
- --------------------------------------------------
- [[sql-functions-now]]
- ==== `NOW`
- .Synopsis:
- [source, sql]
- --------------------------------------------------
- NOW()
- --------------------------------------------------
- *Input*: _none_
- *Output*: datetime
- .Description:
- This function offers the same functionality as <<sql-functions-current-timestamp,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.
- ["source","sql",subs="attributes,callouts,macros"]
- --------------------------------------------------
- include-tagged::{sql-specs}/docs/docs.csv-spec[nowFunction]
- --------------------------------------------------
- Typically, this function (as well as its twin <<sql-functions-current-timestamp,CURRENT_TIMESTAMP())>> function is used
- for relative date/time filtering:
- ["source","sql",subs="attributes,callouts,macros"]
- --------------------------------------------------
- include-tagged::{sql-specs}/docs/docs.csv-spec[filterNow]
- --------------------------------------------------
- [[sql-functions-datetime-second]]
- ==== `SECOND_OF_MINUTE/SECOND`
- .Synopsis:
- [source, sql]
- --------------------------------------------------
- SECOND_OF_MINUTE(datetime_exp<1>)
- --------------------------------------------------
- *Input*:
- <1> date/datetime expression
- *Output*: integer
- .Description:
- Extract the second of the minute from a date/datetime.
- ["source","sql",subs="attributes,callouts,macros"]
- --------------------------------------------------
- include-tagged::{sql-specs}/docs/docs.csv-spec[secondOfMinute]
- --------------------------------------------------
- [[sql-functions-datetime-quarter]]
- ==== `QUARTER`
- .Synopsis:
- [source, sql]
- --------------------------------------------------
- QUARTER(datetime_exp<1>)
- --------------------------------------------------
- *Input*:
- <1> date/datetime expression
- *Output*: integer
- .Description:
- Extract the year quarter the date/datetime falls in.
- ["source","sql",subs="attributes,callouts,macros"]
- --------------------------------------------------
- include-tagged::{sql-specs}/docs/docs.csv-spec[quarter]
- --------------------------------------------------
- [[sql-functions-today]]
- ==== `TODAY`
- .Synopsis:
- [source, sql]
- --------------------------------------------------
- TODAY()
- --------------------------------------------------
- *Input*: _none_
- *Output*: date
- .Description:
- This function offers the same functionality as <<sql-functions-current-date,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.
- ["source","sql",subs="attributes,callouts,macros"]
- --------------------------------------------------
- include-tagged::{sql-specs}/docs/docs.csv-spec[todayFunction]
- --------------------------------------------------
- Typically, this function (as well as its twin <<sql-functions-current-timestamp,CURRENT_TIMESTAMP())>> function is used
- for relative date filtering:
- ["source","sql",subs="attributes,callouts,macros"]
- --------------------------------------------------
- include-tagged::{sql-specs}/docs/docs.csv-spec[filterToday]
- --------------------------------------------------
- [[sql-functions-datetime-week]]
- ==== `WEEK_OF_YEAR/WEEK`
- .Synopsis:
- [source, sql]
- --------------------------------------------------
- WEEK_OF_YEAR(datetime_exp<1>)
- --------------------------------------------------
- *Input*:
- <1> date/datetime expression
- *Output*: integer
- .Description:
- Extract the week of the year from a date/datetime.
- ["source","sql",subs="attributes,callouts,macros"]
- --------------------------------------------------
- include-tagged::{sql-specs}/docs/docs.csv-spec[weekOfYear]
- --------------------------------------------------
- [[sql-functions-datetime-year]]
- ==== `YEAR`
- .Synopsis:
- [source, sql]
- --------------------------------------------------
- YEAR(datetime_exp<1>)
- --------------------------------------------------
- *Input*:
- <1> date/datetime expression
- *Output*: integer
- .Description:
- Extract the year from a date/datetime.
- ["source","sql",subs="attributes,callouts,macros"]
- --------------------------------------------------
- include-tagged::{sql-specs}/docs/docs.csv-spec[year]
- --------------------------------------------------
- [[sql-functions-datetime-extract]]
- ==== `EXTRACT`
- .Synopsis:
- [source, sql]
- --------------------------------------------------
- EXTRACT(datetime_function<1> FROM datetime_exp<2>)
- --------------------------------------------------
- *Input*:
- <1> date/time function name
- <2> date/datetime expression
- *Output*: integer
- .Description:
- Extract fields from a date/datetime by specifying the name of a <<sql-functions-datetime,datetime function>>.
- The following
- ["source","sql",subs="attributes,callouts,macros"]
- --------------------------------------------------
- include-tagged::{sql-specs}/docs/docs.csv-spec[extractDayOfYear]
- --------------------------------------------------
- is the equivalent to
- ["source","sql",subs="attributes,callouts,macros"]
- --------------------------------------------------
- include-tagged::{sql-specs}/docs/docs.csv-spec[dayOfYear]
- --------------------------------------------------
|