2018-09-05 18:19:49 -04:00
[role="xpack"]
[testenv="basic"]
[[sql-functions-datetime]]
2018-12-14 11:20:37 -05:00
=== 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
2019-09-11 14:09:40 -04:00
A common requirement when dealing with date/time in general revolves around
2018-12-21 16:25:54 -05:00
the notion of `interval`, a topic that is worth exploring in the context of {es} and {es-sql}.
2018-12-14 11:20:37 -05:00
{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}:
2018-12-21 16:25:54 -05:00
[cols="^m,^m"]
|==========================
s|{es}
s|{es-sql}
2019-01-17 03:17:58 -05:00
2+h| Index/Table datetime math
2018-12-14 11:20:37 -05:00
2+|<index-{now/M{YYYY.MM}}>
2019-01-17 03:17:58 -05:00
2+h| Query date/time math
2018-12-14 11:20:37 -05:00
| 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
2018-12-21 16:25:54 -05:00
|==========================
2018-12-14 11:20:37 -05:00
`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:
2018-12-21 16:25:54 -05:00
[cols="^,^"]
2018-12-14 11:20:37 -05:00
|===
2018-12-21 16:25:54 -05:00
s|Interval
s|Description
2018-12-14 11:20:37 -05:00
| `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
2019-12-02 09:05:05 -05:00
Basic arithmetic operators (`+`, `-`, `*`) support date/time parameters as indicated below:
2018-12-14 11:20:37 -05:00
2019-05-31 13:03:41 -04:00
[source, sql]
2018-12-14 11:20:37 -05:00
--------------------------------------------------
2019-03-25 09:22:59 -04:00
include-tagged::{sql-specs}/docs/docs.csv-spec[dtIntervalPlusInterval]
2018-12-14 11:20:37 -05:00
--------------------------------------------------
2019-05-31 13:03:41 -04:00
[source, sql]
2018-12-14 11:20:37 -05:00
--------------------------------------------------
2019-03-25 09:22:59 -04:00
include-tagged::{sql-specs}/docs/docs.csv-spec[dtDateTimePlusInterval]
2018-12-14 11:20:37 -05:00
--------------------------------------------------
2019-05-31 13:03:41 -04:00
[source, sql]
2018-12-14 11:20:37 -05:00
--------------------------------------------------
2019-03-25 09:22:59 -04:00
include-tagged::{sql-specs}/docs/docs.csv-spec[dtMinusInterval]
2018-12-14 11:20:37 -05:00
--------------------------------------------------
2019-05-31 13:03:41 -04:00
[source, sql]
2018-12-14 11:20:37 -05:00
--------------------------------------------------
2019-03-25 09:22:59 -04:00
include-tagged::{sql-specs}/docs/docs.csv-spec[dtIntervalMinusInterval]
2018-12-14 11:20:37 -05:00
--------------------------------------------------
2019-05-31 13:03:41 -04:00
[source, sql]
2018-12-14 11:20:37 -05:00
--------------------------------------------------
2019-03-25 09:22:59 -04:00
include-tagged::{sql-specs}/docs/docs.csv-spec[dtDateTimeMinusInterval]
2018-12-14 11:20:37 -05:00
--------------------------------------------------
2019-05-31 13:03:41 -04:00
[source, sql]
2018-12-14 11:20:37 -05:00
--------------------------------------------------
2019-03-25 09:22:59 -04:00
include-tagged::{sql-specs}/docs/docs.csv-spec[dtIntervalMul]
2018-12-14 11:20:37 -05:00
--------------------------------------------------
==== Functions
2018-09-05 18:19:49 -04:00
2019-02-09 17:09:03 -05:00
Functions that target date/time.
2018-12-11 05:29:44 -05:00
2019-02-05 11:15:26 -05:00
[[sql-functions-current-date]]
==== `CURRENT_DATE/CURDATE`
.Synopsis:
[source, sql]
--------------------------------------------------
CURRENT_DATE
CURRENT_DATE()
2019-03-30 07:05:15 -04:00
CURDATE()
2019-02-05 11:15:26 -05:00
--------------------------------------------------
*Input*: _none_
*Output*: date
2020-02-14 11:58:45 -05:00
*Description*: Returns the date (no time part) when the current query reached the server.
2019-02-05 11:15:26 -05:00
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.
2019-05-31 13:03:41 -04:00
[source, sql]
2019-02-05 11:15:26 -05:00
--------------------------------------------------
2019-03-30 07:05:15 -04:00
include-tagged::{sql-specs}/docs/docs.csv-spec[currentDate]
--------------------------------------------------
2019-05-31 13:03:41 -04:00
[source, sql]
2019-03-30 07:05:15 -04:00
--------------------------------------------------
include-tagged::{sql-specs}/docs/docs.csv-spec[currentDateFunction]
2019-02-05 11:15:26 -05:00
--------------------------------------------------
2019-05-31 13:03:41 -04:00
[source, sql]
2019-02-05 11:15:26 -05:00
--------------------------------------------------
2019-03-25 09:22:59 -04:00
include-tagged::{sql-specs}/docs/docs.csv-spec[curDateFunction]
2019-02-05 11:15:26 -05:00
--------------------------------------------------
Typically, this function (as well as its twin <<sql-functions-today,TODAY())>> function
is used for relative date filtering:
2019-05-31 13:03:41 -04:00
[source, sql]
2019-02-05 11:15:26 -05:00
--------------------------------------------------
2019-03-25 09:22:59 -04:00
include-tagged::{sql-specs}/docs/docs.csv-spec[filterToday]
2019-02-05 11:15:26 -05:00
--------------------------------------------------
2019-04-03 13:35:37 -04:00
[[sql-functions-current-time]]
==== `CURRENT_TIME/CURTIME`
.Synopsis:
[source, sql]
--------------------------------------------------
CURRENT_TIME
2019-04-22 09:33:55 -04:00
CURRENT_TIME([precision]) <1>
2019-04-03 13:35:37 -04:00
CURTIME
--------------------------------------------------
*Input*:
<1> fractional digits; optional
*Output*: time
2020-02-14 11:58:45 -05:00
*Description*: Returns the time when the current query reached the server.
2019-04-03 13:35:37 -04:00
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.
2019-05-31 13:03:41 -04:00
[source, sql]
2019-04-03 13:35:37 -04:00
--------------------------------------------------
include-tagged::{sql-specs}/docs/docs.csv-spec[currentTime]
--------------------------------------------------
2019-05-31 13:03:41 -04:00
[source, sql]
2019-04-03 13:35:37 -04:00
--------------------------------------------------
include-tagged::{sql-specs}/docs/docs.csv-spec[currentTimeFunction]
--------------------------------------------------
2019-05-31 13:03:41 -04:00
[source, sql]
2019-04-03 13:35:37 -04:00
--------------------------------------------------
include-tagged::{sql-specs}/docs/docs.csv-spec[curTimeFunction]
--------------------------------------------------
2019-05-31 13:03:41 -04:00
[source, sql]
2019-04-03 13:35:37 -04:00
--------------------------------------------------
include-tagged::{sql-specs}/docs/docs.csv-spec[currentTimeFunctionPrecision]
--------------------------------------------------
Typically, this function is used for relative date/time filtering:
2019-05-31 13:03:41 -04:00
[source, sql]
2019-04-03 13:35:37 -04:00
--------------------------------------------------
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).
2018-12-14 08:42:36 -05:00
[[sql-functions-current-timestamp]]
2018-12-21 16:25:54 -05:00
==== `CURRENT_TIMESTAMP`
2018-12-14 08:42:36 -05:00
.Synopsis:
[source, sql]
--------------------------------------------------
CURRENT_TIMESTAMP
2019-04-22 09:33:55 -04:00
CURRENT_TIMESTAMP([precision]) <1>
2018-12-14 08:42:36 -05:00
--------------------------------------------------
*Input*:
2018-12-21 16:25:54 -05:00
<1> fractional digits; optional
2018-12-14 08:42:36 -05:00
*Output*: date/time
2020-02-14 11:58:45 -05:00
*Description*: Returns the date/time when the current query reached the server.
2018-12-14 08:42:36 -05:00
As a function, `CURRENT_TIMESTAMP()` accepts _precision_ as an optional
2019-02-27 02:48:10 -05:00
parameter for rounding the second fractional digits (nanoseconds). The default _precision_ is 3,
meaning a milliseconds precision current date/time will be returned.
2018-12-14 08:42:36 -05:00
2019-02-05 11:15:26 -05:00
This method always returns the same value for its every occurrence within the same query.
2018-12-14 08:42:36 -05:00
2019-05-31 13:03:41 -04:00
[source, sql]
2018-12-14 08:42:36 -05:00
--------------------------------------------------
2019-03-25 09:22:59 -04:00
include-tagged::{sql-specs}/docs/docs.csv-spec[curTs]
2018-12-14 08:42:36 -05:00
--------------------------------------------------
2019-05-31 13:03:41 -04:00
[source, sql]
2018-12-14 08:42:36 -05:00
--------------------------------------------------
2019-03-25 09:22:59 -04:00
include-tagged::{sql-specs}/docs/docs.csv-spec[curTsFunction]
2018-12-14 08:42:36 -05:00
--------------------------------------------------
2019-05-31 13:03:41 -04:00
[source, sql]
2018-12-14 08:42:36 -05:00
--------------------------------------------------
2019-03-25 09:22:59 -04:00
include-tagged::{sql-specs}/docs/docs.csv-spec[curTsFunctionPrecision]
2018-12-14 08:42:36 -05:00
--------------------------------------------------
2018-12-21 16:25:54 -05:00
Typically, this function (as well as its twin <<sql-functions-now,NOW())>> function is used for
relative date/time filtering:
2018-12-14 08:42:36 -05:00
2019-05-31 13:03:41 -04:00
[source, sql]
2018-12-14 08:42:36 -05:00
--------------------------------------------------
2019-03-25 09:22:59 -04:00
include-tagged::{sql-specs}/docs/docs.csv-spec[filterNow]
2018-12-14 08:42:36 -05:00
--------------------------------------------------
2019-04-03 07:28:30 -04:00
[IMPORTANT]
2019-04-04 05:31:28 -04:00
Currently, using a _precision_ greater than 3 doesn't make any difference to the output of the
2019-04-03 07:28:30 -04:00
function as the maximum number of second fractional digits returned is 3 (milliseconds).
2019-10-10 09:24:36 -04:00
[[sql-functions-datetime-add]]
2019-10-15 08:42:20 -04:00
==== `DATE_ADD/DATEADD/TIMESTAMP_ADD/TIMESTAMPADD`
2019-10-10 09:24:36 -04:00
.Synopsis:
[source, sql]
--------------------------------------------------
DATE_ADD(
string_exp, <1>
integer_exp, <2>
datetime_exp) <3>
--------------------------------------------------
*Input*:
<1> string expression denoting the date/time unit to add to the date/datetime
<2> 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
<3> date/datetime expression
*Output*: datetime
2020-02-14 11:58:45 -05:00
*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
2019-10-10 09:24:36 -04:00
the date/datetime. If any of the three arguments is `null` a `null` is returned.
2019-10-15 06:25:04 -04:00
[WARNING]
If the second argument is a long there is possibility of truncation since an integer value will be extracted and
used from that long.
2019-10-10 09:24:36 -04:00
[cols="^,^"]
|===
2+h|Datetime units to add/subtract
s|unit
s|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
|===
[source, sql]
--------------------------------------------------
include-tagged::{sql-specs}/docs/docs.csv-spec[dateAddDateTimeYears]
--------------------------------------------------
[source, sql]
--------------------------------------------------
include-tagged::{sql-specs}/docs/docs.csv-spec[dateAddDateTimeWeeks]
--------------------------------------------------
[source, sql]
--------------------------------------------------
include-tagged::{sql-specs}/docs/docs.csv-spec[dateAddDateTimeSeconds]
--------------------------------------------------
[source, sql]
--------------------------------------------------
include-tagged::{sql-specs}/docs/docs.csv-spec[dateAddDateQuarters]
--------------------------------------------------
[source, sql]
--------------------------------------------------
include-tagged::{sql-specs}/docs/docs.csv-spec[dateAddDateMinutes]
--------------------------------------------------
2019-10-01 08:59:25 -04:00
2019-10-15 08:42:20 -04:00
[[sql-functions-datetime-diff]]
==== `DATE_DIFF/DATEDIFF/TIMESTAMP_DIFF/TIMESTAMPDIFF`
.Synopsis:
[source, sql]
--------------------------------------------------
DATE_DIFF(
string_exp, <1>
datetime_exp, <2>
datetime_exp) <3>
--------------------------------------------------
*Input*:
<1> string expression denoting the date/time unit difference between the following two date/datetime expressions
<2> start date/datetime expression
<3> end date/datetime expression
*Output*: integer
2020-02-14 11:58:45 -05:00
*Description*: Subtract the second argument from the third argument and return their difference in multiples of the unit
2019-10-15 08:42:20 -04:00
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.
[cols="^,^"]
|===
2+h|Datetime difference units
s|unit
s|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
|===
[source, sql]
--------------------------------------------------
include-tagged::{sql-specs}/docs/docs.csv-spec[dateDiffDateTimeYears]
--------------------------------------------------
[source, sql]
--------------------------------------------------
include-tagged::{sql-specs}/docs/docs.csv-spec[dateDiffDateTimeWeeks]
--------------------------------------------------
[source, sql]
--------------------------------------------------
include-tagged::{sql-specs}/docs/docs.csv-spec[dateDiffDateTimeSeconds]
--------------------------------------------------
[source, sql]
--------------------------------------------------
include-tagged::{sql-specs}/docs/docs.csv-spec[dateDiffDateQuarters]
--------------------------------------------------
2019-11-19 07:40:34 -05:00
[NOTE]
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.
[source, sql]
--------------------------------------------------
include-tagged::{sql-specs}/docs/docs.csv-spec[dateDiffDateTimeHours]
--------------------------------------------------
[source, sql]
--------------------------------------------------
include-tagged::{sql-specs}/docs/docs.csv-spec[dateDiffDateTimeMinutes]
--------------------------------------------------
2019-10-15 08:42:20 -04:00
[source, sql]
--------------------------------------------------
include-tagged::{sql-specs}/docs/docs.csv-spec[dateDiffDateMinutes]
--------------------------------------------------
2020-04-08 07:45:47 -04:00
[[sql-functions-datetime-datetimeformat]]
==== `DATETIME_FORMAT`
.Synopsis:
[source, sql]
--------------------------------------------------
DATETIME_FORMAT(
date_exp/datetime_exp/time_exp, <1>
string_exp) <2>
--------------------------------------------------
*Input*:
<1> date/datetime/time expression
<2> 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
https://docs.oracle.com/en/java/javase/14/docs/api/java.base/java/time/format/DateTimeFormatter.html[`java.time.format.DateTimeFormatter`].
If any of the two arguments is `null` or the pattern is an empty string `null` is returned.
2020-04-09 19:16:29 -04:00
[NOTE]
2020-04-08 07:45:47 -04:00
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.
[source, sql]
--------------------------------------------------
include-tagged::{sql-specs}/docs/docs.csv-spec[dateTimeFormatDate]
--------------------------------------------------
[source, sql]
--------------------------------------------------
include-tagged::{sql-specs}/docs/docs.csv-spec[dateTimeFormatDateTime]
--------------------------------------------------
[source, sql]
--------------------------------------------------
include-tagged::{sql-specs}/docs/docs.csv-spec[dateTimeFormatTime]
--------------------------------------------------
2020-04-09 19:16:29 -04:00
[[sql-functions-datetime-datetimeparse]]
==== `DATETIME_PARSE`
.Synopsis:
[source, sql]
--------------------------------------------------
DATETIME_PARSE(
string_exp, <1>
string_exp) <2>
--------------------------------------------------
*Input*:
<1> datetime expression as a string
<2> 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
https://docs.oracle.com/en/java/javase/14/docs/api/java.base/java/time/format/DateTimeFormatter.html[`java.time.format.DateTimeFormatter`].
If any of the two arguments is `null` or an empty string `null` is returned.
[NOTE]
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.
[source, sql]
--------------------------------------------------
include-tagged::{sql-specs}/docs/docs.csv-spec[dateTimeParse1]
--------------------------------------------------
[source, sql]
--------------------------------------------------
include-tagged::{sql-specs}/docs/docs.csv-spec[dateTimeParse2]
--------------------------------------------------
2020-05-05 06:08:39 -04:00
[NOTE]
====
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 <<sql-rest-fields-timezone,`time_zone`>>/<<jdbc-cfg-timezone,`timezone`>> REST/driver parameters
with no conversion applied.
[source, sql]
--------------------------------------------------
include-tagged::{sql-specs}/docs/docs.csv-spec[dateTimeParse3]
--------------------------------------------------
====
2019-10-01 08:59:25 -04:00
[[sql-functions-datetime-part]]
==== `DATE_PART/DATEPART`
.Synopsis:
[source, sql]
--------------------------------------------------
DATE_PART(
string_exp, <1>
datetime_exp) <2>
--------------------------------------------------
*Input*:
<1> string expression denoting the unit to extract from the date/datetime
<2> date/datetime expression
*Output*: integer
2020-02-14 11:58:45 -05:00
*Description*: Extract the specified unit from a date/datetime. If any of the two arguments is `null` a `null` is returned.
2019-10-01 08:59:25 -04:00
It's similar to <<sql-functions-datetime-extract>> but with different names and aliases for the units and
provides more options (e.g.: `TZOFFSET`).
[cols="^,^"]
|===
2+h|Datetime units to extract
s|unit
s|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
|===
[source, sql]
--------------------------------------------------
include-tagged::{sql-specs}/docs/docs.csv-spec[datePartDateTimeYears]
--------------------------------------------------
[source, sql]
--------------------------------------------------
include-tagged::{sql-specs}/docs/docs.csv-spec[datePartDateTimeMinutes]
--------------------------------------------------
[source, sql]
--------------------------------------------------
include-tagged::{sql-specs}/docs/docs.csv-spec[datePartDateQuarter]
--------------------------------------------------
[source, sql]
--------------------------------------------------
include-tagged::{sql-specs}/docs/docs.csv-spec[datePartDateMonth]
--------------------------------------------------
[NOTE]
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.
[source, sql]
--------------------------------------------------
include-tagged::{sql-specs}/docs/docs.csv-spec[datePartDateTimeWeek]
--------------------------------------------------
[NOTE]
The `tzoffset` returns the total number of minutes (signed) that represent the time zone's offset.
[source, sql]
--------------------------------------------------
include-tagged::{sql-specs}/docs/docs.csv-spec[datePartDateTimeTzOffsetPlus]
--------------------------------------------------
[source, sql]
--------------------------------------------------
include-tagged::{sql-specs}/docs/docs.csv-spec[datePartDateTimeTzOffsetMinus]
--------------------------------------------------
2019-09-11 14:09:40 -04:00
[[sql-functions-datetime-trunc]]
2019-09-27 08:38:03 -04:00
==== `DATE_TRUNC/DATETRUNC`
2019-09-11 14:09:40 -04:00
.Synopsis:
[source, sql]
--------------------------------------------------
DATE_TRUNC(
string_exp, <1>
2020-03-23 10:05:16 -04:00
datetime_exp/interval_exp) <2>
2019-09-11 14:09:40 -04:00
--------------------------------------------------
*Input*:
2020-03-23 10:05:16 -04:00
<1> string expression denoting the unit to which the date/datetime/interval should be truncated to
<2> date/datetime/interval expression
2019-09-11 14:09:40 -04:00
2020-03-23 10:05:16 -04:00
*Output*: datetime/interval
2019-09-11 14:09:40 -04:00
2020-03-23 10:05:16 -04:00
*Description*: Truncate the date/datetime/interval to the specified unit by setting all fields that are less significant than the specified
2019-09-27 08:38:03 -04:00
one to zero (or one, for day, day of week and month). If any of the two arguments is `null` a `null` is returned.
2020-03-23 10:05:16 -04:00
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.
2019-09-11 14:09:40 -04:00
[cols="^,^"]
|===
2+h|Datetime truncation units
s|unit
s|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
|===
[source, sql]
--------------------------------------------------
include-tagged::{sql-specs}/docs/docs.csv-spec[truncateDateTimeMillennium]
--------------------------------------------------
[source, sql]
--------------------------------------------------
include-tagged::{sql-specs}/docs/docs.csv-spec[truncateDateTimeWeek]
--------------------------------------------------
[source, sql]
--------------------------------------------------
include-tagged::{sql-specs}/docs/docs.csv-spec[truncateDateTimeMinutes]
--------------------------------------------------
[source, sql]
--------------------------------------------------
include-tagged::{sql-specs}/docs/docs.csv-spec[truncateDateDecades]
--------------------------------------------------
[source, sql]
--------------------------------------------------
include-tagged::{sql-specs}/docs/docs.csv-spec[truncateDateQuarter]
--------------------------------------------------
2020-03-23 10:05:16 -04:00
[source, sql]
--------------------------------------------------
include-tagged::{sql-specs}/docs/docs.csv-spec[truncateIntervalCenturies]
--------------------------------------------------
[source, sql]
--------------------------------------------------
include-tagged::{sql-specs}/docs/docs.csv-spec[truncateIntervalHour]
--------------------------------------------------
[source, sql]
--------------------------------------------------
include-tagged::{sql-specs}/docs/docs.csv-spec[truncateIntervalDay]
--------------------------------------------------
2018-12-11 19:29:02 -05:00
[[sql-functions-datetime-day]]
2018-12-21 16:25:54 -05:00
==== `DAY_OF_MONTH/DOM/DAY`
2018-12-11 19:29:02 -05:00
.Synopsis:
[source, sql]
--------------------------------------------------
2019-04-22 09:33:55 -04:00
DAY_OF_MONTH(datetime_exp) <1>
2018-12-11 19:29:02 -05:00
--------------------------------------------------
*Input*:
2019-01-17 03:17:58 -05:00
<1> date/datetime expression
2018-12-11 19:29:02 -05:00
*Output*: integer
2020-02-14 11:58:45 -05:00
*Description*: Extract the day of the month from a date/datetime.
2018-09-05 18:19:49 -04:00
2019-05-31 13:03:41 -04:00
[source, sql]
2018-09-05 18:19:49 -04:00
--------------------------------------------------
2019-03-25 09:22:59 -04:00
include-tagged::{sql-specs}/docs/docs.csv-spec[dayOfMonth]
2018-12-11 19:29:02 -05:00
--------------------------------------------------
[[sql-functions-datetime-dow]]
2018-12-21 16:25:54 -05:00
==== `DAY_OF_WEEK/DAYOFWEEK/DOW`
2018-12-11 19:29:02 -05:00
.Synopsis:
[source, sql]
--------------------------------------------------
2019-04-22 09:33:55 -04:00
DAY_OF_WEEK(datetime_exp) <1>
2018-09-05 18:19:49 -04:00
--------------------------------------------------
2018-12-11 19:29:02 -05:00
*Input*:
2019-01-17 03:17:58 -05:00
<1> date/datetime expression
2018-12-11 19:29:02 -05:00
*Output*: integer
2020-02-14 11:58:45 -05:00
*Description*: Extract the day of the week from a date/datetime. Sunday is `1`, Monday is `2`, etc.
2018-09-05 18:19:49 -04:00
2019-05-31 13:03:41 -04:00
[source, sql]
2018-09-05 18:19:49 -04:00
--------------------------------------------------
2019-03-25 09:22:59 -04:00
include-tagged::{sql-specs}/docs/docs.csv-spec[dayOfWeek]
2018-12-11 19:29:02 -05:00
--------------------------------------------------
[[sql-functions-datetime-doy]]
2018-12-21 16:25:54 -05:00
==== `DAY_OF_YEAR/DOY`
2018-12-11 19:29:02 -05:00
.Synopsis:
[source, sql]
--------------------------------------------------
2019-04-22 09:33:55 -04:00
DAY_OF_YEAR(datetime_exp) <1>
2018-09-05 18:19:49 -04:00
--------------------------------------------------
2018-12-11 19:29:02 -05:00
*Input*:
2019-01-17 03:17:58 -05:00
<1> date/datetime expression
2018-12-11 19:29:02 -05:00
*Output*: integer
2020-02-14 11:58:45 -05:00
*Description*: Extract the day of the year from a date/datetime.
2018-09-05 18:19:49 -04:00
2019-05-31 13:03:41 -04:00
[source, sql]
2018-09-05 18:19:49 -04:00
--------------------------------------------------
2019-03-25 09:22:59 -04:00
include-tagged::{sql-specs}/docs/docs.csv-spec[dayOfYear]
2018-12-11 19:29:02 -05:00
--------------------------------------------------
[[sql-functions-datetime-dayname]]
2018-12-21 16:25:54 -05:00
==== `DAY_NAME/DAYNAME`
2018-12-11 19:29:02 -05:00
.Synopsis:
[source, sql]
--------------------------------------------------
2019-04-22 09:33:55 -04:00
DAY_NAME(datetime_exp) <1>
2018-09-05 18:19:49 -04:00
--------------------------------------------------
2018-12-11 19:29:02 -05:00
*Input*:
2019-01-17 03:17:58 -05:00
<1> date/datetime expression
2018-12-11 19:29:02 -05:00
*Output*: string
2020-02-14 11:58:45 -05:00
*Description*: Extract the day of the week from a date/datetime in text format (`Monday`, `Tuesday`...).
2018-09-05 18:19:49 -04:00
2019-05-31 13:03:41 -04:00
[source, sql]
2018-09-05 18:19:49 -04:00
--------------------------------------------------
2019-03-25 09:22:59 -04:00
include-tagged::{sql-specs}/docs/docs.csv-spec[dayName]
2018-12-11 19:29:02 -05:00
--------------------------------------------------
[[sql-functions-datetime-hour]]
2018-12-21 16:25:54 -05:00
==== `HOUR_OF_DAY/HOUR`
2018-12-11 19:29:02 -05:00
.Synopsis:
[source, sql]
2018-09-05 18:19:49 -04:00
--------------------------------------------------
2019-04-22 09:33:55 -04:00
HOUR_OF_DAY(datetime_exp) <1>
2018-12-11 19:29:02 -05:00
--------------------------------------------------
*Input*:
2019-01-17 03:17:58 -05:00
<1> date/datetime expression
2018-12-11 19:29:02 -05:00
*Output*: integer
2018-09-05 18:19:49 -04:00
2020-02-14 11:58:45 -05:00
*Description*: Extract the hour of the day from a date/datetime.
2018-09-05 18:19:49 -04:00
2019-05-31 13:03:41 -04:00
[source, sql]
2018-09-05 18:19:49 -04:00
--------------------------------------------------
2019-03-25 09:22:59 -04:00
include-tagged::{sql-specs}/docs/docs.csv-spec[hourOfDay]
2018-12-11 19:29:02 -05:00
--------------------------------------------------
[[sql-functions-datetime-isodow]]
2018-12-21 16:25:54 -05:00
==== `ISO_DAY_OF_WEEK/ISODAYOFWEEK/ISODOW/IDOW`
2018-12-11 19:29:02 -05:00
.Synopsis:
[source, sql]
--------------------------------------------------
2019-04-22 09:33:55 -04:00
ISO_DAY_OF_WEEK(datetime_exp) <1>
2018-09-05 18:19:49 -04:00
--------------------------------------------------
2018-12-11 19:29:02 -05:00
*Input*:
2019-01-17 03:17:58 -05:00
<1> date/datetime expression
2018-12-11 19:29:02 -05:00
*Output*: integer
2020-02-14 11:58:45 -05:00
*Description*: Extract the day of the week from a date/datetime, following the https://en.wikipedia.org/wiki/ISO_week_date[ISO 8601 standard].
2018-09-05 18:19:49 -04:00
Monday is `1`, Tuesday is `2`, etc.
2019-05-31 13:03:41 -04:00
[source, sql]
2018-09-05 18:19:49 -04:00
--------------------------------------------------
2019-03-25 09:22:59 -04:00
include-tagged::{sql-specs}/docs/docs.csv-spec[isoDayOfWeek]
2018-09-05 18:19:49 -04:00
--------------------------------------------------
2018-12-11 19:29:02 -05:00
[[sql-functions-datetime-isoweek]]
2018-12-21 16:25:54 -05:00
==== `ISO_WEEK_OF_YEAR/ISOWEEKOFYEAR/ISOWEEK/IWOY/IW`
2018-12-11 19:29:02 -05:00
.Synopsis:
[source, sql]
--------------------------------------------------
2019-04-22 09:33:55 -04:00
ISO_WEEK_OF_YEAR(datetime_exp) <1>
2018-12-11 19:29:02 -05:00
--------------------------------------------------
*Input*:
2019-01-17 03:17:58 -05:00
<1> date/datetime expression
2018-12-11 19:29:02 -05:00
*Output*: integer
2020-02-14 11:58:45 -05:00
*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
2018-12-11 19:29:02 -05:00
of a year is the first week with a majority (4 or more) of its days in January.
2018-09-05 18:19:49 -04:00
2019-05-31 13:03:41 -04:00
[source, sql]
2018-09-05 18:19:49 -04:00
--------------------------------------------------
2019-03-25 09:22:59 -04:00
include-tagged::{sql-specs}/docs/docs.csv-spec[isoWeekOfYear]
2018-12-11 19:29:02 -05:00
--------------------------------------------------
[[sql-functions-datetime-minuteofday]]
==== `MINUTE_OF_DAY`
.Synopsis:
[source, sql]
--------------------------------------------------
2019-04-22 09:33:55 -04:00
MINUTE_OF_DAY(datetime_exp) <1>
2018-09-05 18:19:49 -04:00
--------------------------------------------------
2018-12-11 19:29:02 -05:00
*Input*:
2019-01-17 03:17:58 -05:00
<1> date/datetime expression
2018-12-11 19:29:02 -05:00
*Output*: integer
2020-02-14 11:58:45 -05:00
*Description*: Extract the minute of the day from a date/datetime.
2018-09-05 18:19:49 -04:00
2019-05-31 13:03:41 -04:00
[source, sql]
2018-09-05 18:19:49 -04:00
--------------------------------------------------
2019-03-25 09:22:59 -04:00
include-tagged::{sql-specs}/docs/docs.csv-spec[minuteOfDay]
2018-09-05 18:19:49 -04:00
--------------------------------------------------
2018-12-11 19:29:02 -05:00
[[sql-functions-datetime-minute]]
2018-12-21 16:25:54 -05:00
==== `MINUTE_OF_HOUR/MINUTE`
2018-12-11 19:29:02 -05:00
.Synopsis:
[source, sql]
--------------------------------------------------
2019-04-22 09:33:55 -04:00
MINUTE_OF_HOUR(datetime_exp) <1>
2018-12-11 19:29:02 -05:00
--------------------------------------------------
*Input*:
2019-01-17 03:17:58 -05:00
<1> date/datetime expression
2018-12-11 19:29:02 -05:00
*Output*: integer
2020-02-14 11:58:45 -05:00
*Description*: Extract the minute of the hour from a date/datetime.
2018-09-05 18:19:49 -04:00
2019-05-31 13:03:41 -04:00
[source, sql]
2018-09-05 18:19:49 -04:00
--------------------------------------------------
2019-03-25 09:22:59 -04:00
include-tagged::{sql-specs}/docs/docs.csv-spec[minuteOfHour]
2018-12-11 19:29:02 -05:00
--------------------------------------------------
[[sql-functions-datetime-month]]
2018-12-21 16:25:54 -05:00
==== `MONTH_OF_YEAR/MONTH`
2018-12-11 19:29:02 -05:00
.Synopsis:
[source, sql]
--------------------------------------------------
2019-04-22 09:33:55 -04:00
MONTH(datetime_exp) <1>
2018-09-05 18:19:49 -04:00
--------------------------------------------------
2018-12-11 19:29:02 -05:00
*Input*:
2019-01-17 03:17:58 -05:00
<1> date/datetime expression
2018-12-11 19:29:02 -05:00
*Output*: integer
2020-02-14 11:58:45 -05:00
*Description*: Extract the month of the year from a date/datetime.
2018-12-11 19:29:02 -05:00
2019-05-31 13:03:41 -04:00
[source, sql]
2018-12-11 19:29:02 -05:00
--------------------------------------------------
2019-03-25 09:22:59 -04:00
include-tagged::{sql-specs}/docs/docs.csv-spec[monthOfYear]
2018-12-11 19:29:02 -05:00
--------------------------------------------------
[[sql-functions-datetime-monthname]]
2018-12-21 16:25:54 -05:00
==== `MONTH_NAME/MONTHNAME`
2018-12-11 19:29:02 -05:00
.Synopsis:
[source, sql]
--------------------------------------------------
2019-04-22 09:33:55 -04:00
MONTH_NAME(datetime_exp) <1>
2018-12-11 19:29:02 -05:00
--------------------------------------------------
*Input*:
2019-01-17 03:17:58 -05:00
<1> date/datetime expression
2018-12-11 19:29:02 -05:00
*Output*: string
2020-02-14 11:58:45 -05:00
*Description*: Extract the month from a date/datetime in text format (`January`, `February`...).
2018-12-11 19:29:02 -05:00
2019-05-31 13:03:41 -04:00
[source, sql]
2018-12-11 19:29:02 -05:00
--------------------------------------------------
2019-03-25 09:22:59 -04:00
include-tagged::{sql-specs}/docs/docs.csv-spec[monthName]
2018-12-11 19:29:02 -05:00
--------------------------------------------------
2018-12-21 16:25:54 -05:00
[[sql-functions-now]]
==== `NOW`
.Synopsis:
[source, sql]
--------------------------------------------------
NOW()
--------------------------------------------------
*Input*: _none_
2019-01-17 03:17:58 -05:00
*Output*: datetime
2018-12-21 16:25:54 -05:00
2020-02-14 11:58:45 -05:00
*Description*: This function offers the same functionality as <<sql-functions-current-timestamp,CURRENT_TIMESTAMP()>> function: returns
2019-02-05 11:15:26 -05:00
the datetime when the current query reached the server. This method always returns the same value for its every
occurrence within the same query.
2018-12-21 16:25:54 -05:00
2019-05-31 13:03:41 -04:00
[source, sql]
2018-12-21 16:25:54 -05:00
--------------------------------------------------
2019-03-25 09:22:59 -04:00
include-tagged::{sql-specs}/docs/docs.csv-spec[nowFunction]
2018-12-21 16:25:54 -05:00
--------------------------------------------------
2019-01-17 03:17:58 -05:00
Typically, this function (as well as its twin <<sql-functions-current-timestamp,CURRENT_TIMESTAMP())>> function is used
for relative date/time filtering:
2018-12-21 16:25:54 -05:00
2019-05-31 13:03:41 -04:00
[source, sql]
2018-12-21 16:25:54 -05:00
--------------------------------------------------
2019-03-25 09:22:59 -04:00
include-tagged::{sql-specs}/docs/docs.csv-spec[filterNow]
2018-12-21 16:25:54 -05:00
--------------------------------------------------
2018-12-11 19:29:02 -05:00
[[sql-functions-datetime-second]]
2018-12-21 16:25:54 -05:00
==== `SECOND_OF_MINUTE/SECOND`
2018-12-11 19:29:02 -05:00
.Synopsis:
[source, sql]
--------------------------------------------------
2019-04-22 09:33:55 -04:00
SECOND_OF_MINUTE(datetime_exp) <1>
2018-12-11 19:29:02 -05:00
--------------------------------------------------
*Input*:
2019-01-17 03:17:58 -05:00
<1> date/datetime expression
2018-12-11 19:29:02 -05:00
*Output*: integer
2020-02-14 11:58:45 -05:00
*Description*: Extract the second of the minute from a date/datetime.
2018-12-11 19:29:02 -05:00
2019-05-31 13:03:41 -04:00
[source, sql]
2018-12-11 19:29:02 -05:00
--------------------------------------------------
2019-03-25 09:22:59 -04:00
include-tagged::{sql-specs}/docs/docs.csv-spec[secondOfMinute]
2018-12-11 19:29:02 -05:00
--------------------------------------------------
[[sql-functions-datetime-quarter]]
==== `QUARTER`
.Synopsis:
[source, sql]
--------------------------------------------------
2019-04-22 09:33:55 -04:00
QUARTER(datetime_exp) <1>
2018-12-11 19:29:02 -05:00
--------------------------------------------------
*Input*:
2019-01-17 03:17:58 -05:00
<1> date/datetime expression
2018-12-11 19:29:02 -05:00
*Output*: integer
2020-02-14 11:58:45 -05:00
*Description*: Extract the year quarter the date/datetime falls in.
2018-12-11 19:29:02 -05:00
2019-05-31 13:03:41 -04:00
[source, sql]
2018-12-11 19:29:02 -05:00
--------------------------------------------------
2019-03-25 09:22:59 -04:00
include-tagged::{sql-specs}/docs/docs.csv-spec[quarter]
2018-12-11 19:29:02 -05:00
--------------------------------------------------
2019-02-05 11:15:26 -05:00
[[sql-functions-today]]
==== `TODAY`
.Synopsis:
[source, sql]
--------------------------------------------------
TODAY()
--------------------------------------------------
*Input*: _none_
*Output*: date
2020-02-14 11:58:45 -05:00
*Description*: This function offers the same functionality as <<sql-functions-current-date,CURRENT_DATE()>> function: returns
2019-02-05 11:15:26 -05:00
the date when the current query reached the server. This method always returns the same value for its every occurrence
within the same query.
2019-05-31 13:03:41 -04:00
[source, sql]
2019-02-05 11:15:26 -05:00
--------------------------------------------------
2019-03-25 09:22:59 -04:00
include-tagged::{sql-specs}/docs/docs.csv-spec[todayFunction]
2019-02-05 11:15:26 -05:00
--------------------------------------------------
Typically, this function (as well as its twin <<sql-functions-current-timestamp,CURRENT_TIMESTAMP())>> function is used
for relative date filtering:
2019-05-31 13:03:41 -04:00
[source, sql]
2019-02-05 11:15:26 -05:00
--------------------------------------------------
2019-03-25 09:22:59 -04:00
include-tagged::{sql-specs}/docs/docs.csv-spec[filterToday]
2019-02-05 11:15:26 -05:00
--------------------------------------------------
2018-12-11 19:29:02 -05:00
[[sql-functions-datetime-week]]
2018-12-21 16:25:54 -05:00
==== `WEEK_OF_YEAR/WEEK`
2018-12-11 19:29:02 -05:00
.Synopsis:
[source, sql]
--------------------------------------------------
2019-04-22 09:33:55 -04:00
WEEK_OF_YEAR(datetime_exp) <1>
2018-12-11 19:29:02 -05:00
--------------------------------------------------
*Input*:
2019-01-17 03:17:58 -05:00
<1> date/datetime expression
2018-12-11 19:29:02 -05:00
*Output*: integer
2020-02-14 11:58:45 -05:00
*Description*: Extract the week of the year from a date/datetime.
2018-12-11 19:29:02 -05:00
2019-05-31 13:03:41 -04:00
[source, sql]
2018-12-11 19:29:02 -05:00
--------------------------------------------------
2019-03-25 09:22:59 -04:00
include-tagged::{sql-specs}/docs/docs.csv-spec[weekOfYear]
2018-12-11 19:29:02 -05:00
--------------------------------------------------
[[sql-functions-datetime-year]]
==== `YEAR`
.Synopsis:
[source, sql]
--------------------------------------------------
2019-04-22 09:33:55 -04:00
YEAR(datetime_exp) <1>
2018-12-11 19:29:02 -05:00
--------------------------------------------------
*Input*:
2019-01-17 03:17:58 -05:00
<1> date/datetime expression
2018-12-11 19:29:02 -05:00
*Output*: integer
2020-02-14 11:58:45 -05:00
*Description*: Extract the year from a date/datetime.
2018-09-05 18:19:49 -04:00
2019-05-31 13:03:41 -04:00
[source, sql]
2018-09-05 18:19:49 -04:00
--------------------------------------------------
2019-03-25 09:22:59 -04:00
include-tagged::{sql-specs}/docs/docs.csv-spec[year]
2018-12-11 19:29:02 -05:00
--------------------------------------------------
[[sql-functions-datetime-extract]]
==== `EXTRACT`
.Synopsis:
[source, sql]
2018-09-05 18:19:49 -04:00
--------------------------------------------------
2019-04-22 09:33:55 -04:00
EXTRACT(
datetime_function <1>
FROM datetime_exp) <2>
2018-12-11 19:29:02 -05:00
--------------------------------------------------
*Input*:
2019-01-17 03:17:58 -05:00
<1> date/time function name
<2> date/datetime expression
2018-12-11 19:29:02 -05:00
*Output*: integer
2018-09-05 18:19:49 -04:00
2020-02-14 11:58:45 -05:00
*Description*: Extract fields from a date/datetime by specifying the name of a <<sql-functions-datetime,datetime function>>.
2018-12-11 19:29:02 -05:00
The following
2018-09-05 18:19:49 -04:00
2019-05-31 13:03:41 -04:00
[source, sql]
2018-09-05 18:19:49 -04:00
--------------------------------------------------
2019-03-25 09:22:59 -04:00
include-tagged::{sql-specs}/docs/docs.csv-spec[extractDayOfYear]
2018-09-05 18:19:49 -04:00
--------------------------------------------------
is the equivalent to
2019-05-31 13:03:41 -04:00
[source, sql]
2018-09-05 18:19:49 -04:00
--------------------------------------------------
2019-03-25 09:22:59 -04:00
include-tagged::{sql-specs}/docs/docs.csv-spec[dayOfYear]
2018-12-11 05:29:44 -05:00
--------------------------------------------------