date-time.asciidoc 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651
  1. [role="xpack"]
  2. [testenv="basic"]
  3. [[sql-functions-datetime]]
  4. === Date/Time and Interval Functions and Operators
  5. {es-sql} offers a wide range of facilities for performing date/time manipulations.
  6. [[sql-functions-datetime-interval]]
  7. ==== Intervals
  8. A common requirement when dealing with date/time in general revolves around
  9. the notion of `interval`, a topic that is worth exploring in the context of {es} and {es-sql}.
  10. {es} has comprehensive support for <<date-math, date math>> both inside <<date-math-index-names, index names>> and <<mapping-date-format, queries>>.
  11. 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`.
  12. The table below shows the mapping between {es} and {es-sql}:
  13. [cols="^m,^m"]
  14. |==========================
  15. s|{es}
  16. s|{es-sql}
  17. 2+h| Index/Table datetime math
  18. 2+|<index-{now/M{YYYY.MM}}>
  19. 2+h| Query date/time math
  20. | 1y | INTERVAL 1 YEAR
  21. | 2M | INTERVAL 2 MONTH
  22. | 3w | INTERVAL 21 DAY
  23. | 4d | INTERVAL 4 DAY
  24. | 5h | INTERVAL 5 HOUR
  25. | 6m | INTERVAL 6 MINUTE
  26. | 7s | INTERVAL 7 SECOND
  27. |==========================
  28. `INTERVAL` allows either `YEAR` and `MONTH` to be mixed together _or_ `DAY`, `HOUR`, `MINUTE` and `SECOND`.
  29. TIP: {es-sql} accepts also the plural for each time unit (e.g. both `YEAR` and `YEARS` are valid).
  30. Example of the possible combinations below:
  31. [cols="^,^"]
  32. |===
  33. s|Interval
  34. s|Description
  35. | `INTERVAL '1-2' YEAR TO MONTH` | 1 year and 2 months
  36. | `INTERVAL '3 4' DAYS TO HOURS` | 3 days and 4 hours
  37. | `INTERVAL '5 6:12' DAYS TO MINUTES` | 5 days, 6 hours and 12 minutes
  38. | `INTERVAL '3 4:56:01' DAY TO SECOND` | 3 days, 4 hours, 56 minutes and 1 second
  39. | `INTERVAL '2 3:45:01.23456789' DAY TO SECOND` | 2 days, 3 hours, 45 minutes, 1 second and 234567890 nanoseconds
  40. | `INTERVAL '123:45' HOUR TO MINUTES` | 123 hours and 45 minutes
  41. | `INTERVAL '65:43:21.0123' HOUR TO SECONDS` | 65 hours, 43 minutes, 21 seconds and 12300000 nanoseconds
  42. | `INTERVAL '45:01.23' MINUTES TO SECONDS` | 45 minutes, 1 second and 230000000 nanoseconds
  43. |===
  44. ==== Operators
  45. Basic arithmetic operators (`+`, `-`, etc) support date/time parameters as indicated below:
  46. ["source","sql",subs="attributes,callouts,macros"]
  47. --------------------------------------------------
  48. include-tagged::{sql-specs}/docs/docs.csv-spec[dtIntervalPlusInterval]
  49. --------------------------------------------------
  50. ["source","sql",subs="attributes,callouts,macros"]
  51. --------------------------------------------------
  52. include-tagged::{sql-specs}/docs/docs.csv-spec[dtDateTimePlusInterval]
  53. --------------------------------------------------
  54. ["source","sql",subs="attributes,callouts,macros"]
  55. --------------------------------------------------
  56. include-tagged::{sql-specs}/docs/docs.csv-spec[dtMinusInterval]
  57. --------------------------------------------------
  58. ["source","sql",subs="attributes,callouts,macros"]
  59. --------------------------------------------------
  60. include-tagged::{sql-specs}/docs/docs.csv-spec[dtIntervalMinusInterval]
  61. --------------------------------------------------
  62. ["source","sql",subs="attributes,callouts,macros"]
  63. --------------------------------------------------
  64. include-tagged::{sql-specs}/docs/docs.csv-spec[dtDateTimeMinusInterval]
  65. --------------------------------------------------
  66. ["source","sql",subs="attributes,callouts,macros"]
  67. --------------------------------------------------
  68. include-tagged::{sql-specs}/docs/docs.csv-spec[dtIntervalMul]
  69. --------------------------------------------------
  70. ==== Functions
  71. Functions that target date/time.
  72. [[sql-functions-current-date]]
  73. ==== `CURRENT_DATE/CURDATE`
  74. .Synopsis:
  75. [source, sql]
  76. --------------------------------------------------
  77. CURRENT_DATE
  78. CURRENT_DATE()
  79. CURDATE()
  80. --------------------------------------------------
  81. *Input*: _none_
  82. *Output*: date
  83. .Description:
  84. Returns the date (no time part) when the current query reached the server.
  85. It can be used both as a keyword: `CURRENT_DATE` or as a function with no arguments: `CURRENT_DATE()`.
  86. [NOTE]
  87. Unlike CURRENT_DATE, `CURDATE()` can only be used as a function with no arguments and not as a keyword.
  88. This method always returns the same value for its every occurrence within the same query.
  89. ["source","sql",subs="attributes,callouts,macros"]
  90. --------------------------------------------------
  91. include-tagged::{sql-specs}/docs/docs.csv-spec[currentDate]
  92. --------------------------------------------------
  93. ["source","sql",subs="attributes,callouts,macros"]
  94. --------------------------------------------------
  95. include-tagged::{sql-specs}/docs/docs.csv-spec[currentDateFunction]
  96. --------------------------------------------------
  97. ["source","sql",subs="attributes,callouts,macros"]
  98. --------------------------------------------------
  99. include-tagged::{sql-specs}/docs/docs.csv-spec[curDateFunction]
  100. --------------------------------------------------
  101. Typically, this function (as well as its twin <<sql-functions-today,TODAY())>> function
  102. is used for relative date filtering:
  103. ["source","sql",subs="attributes,callouts,macros"]
  104. --------------------------------------------------
  105. include-tagged::{sql-specs}/docs/docs.csv-spec[filterToday]
  106. --------------------------------------------------
  107. [[sql-functions-current-timestamp]]
  108. ==== `CURRENT_TIMESTAMP`
  109. .Synopsis:
  110. [source, sql]
  111. --------------------------------------------------
  112. CURRENT_TIMESTAMP
  113. CURRENT_TIMESTAMP(precision <1>)
  114. --------------------------------------------------
  115. *Input*:
  116. <1> fractional digits; optional
  117. *Output*: date/time
  118. .Description:
  119. Returns the date/time when the current query reached the server.
  120. As a function, `CURRENT_TIMESTAMP()` accepts _precision_ as an optional
  121. parameter for rounding the second fractional digits (nanoseconds). The default _precision_ is 3,
  122. meaning a milliseconds precision current date/time will be returned.
  123. This method always returns the same value for its every occurrence within the same query.
  124. ["source","sql",subs="attributes,callouts,macros"]
  125. --------------------------------------------------
  126. include-tagged::{sql-specs}/docs/docs.csv-spec[curTs]
  127. --------------------------------------------------
  128. ["source","sql",subs="attributes,callouts,macros"]
  129. --------------------------------------------------
  130. include-tagged::{sql-specs}/docs/docs.csv-spec[curTsFunction]
  131. --------------------------------------------------
  132. ["source","sql",subs="attributes,callouts,macros"]
  133. --------------------------------------------------
  134. include-tagged::{sql-specs}/docs/docs.csv-spec[curTsFunctionPrecision]
  135. --------------------------------------------------
  136. Typically, this function (as well as its twin <<sql-functions-now,NOW())>> function is used for
  137. relative date/time filtering:
  138. ["source","sql",subs="attributes,callouts,macros"]
  139. --------------------------------------------------
  140. include-tagged::{sql-specs}/docs/docs.csv-spec[filterNow]
  141. --------------------------------------------------
  142. [IMPORTANT]
  143. Currently, Using a _precision_ greater than 3 doesn't make any difference to the output of the
  144. function as the maximum number of second fractional digits returned is 3 (milliseconds).
  145. [[sql-functions-datetime-day]]
  146. ==== `DAY_OF_MONTH/DOM/DAY`
  147. .Synopsis:
  148. [source, sql]
  149. --------------------------------------------------
  150. DAY_OF_MONTH(datetime_exp<1>)
  151. --------------------------------------------------
  152. *Input*:
  153. <1> date/datetime expression
  154. *Output*: integer
  155. .Description:
  156. Extract the day of the month from a date/datetime.
  157. ["source","sql",subs="attributes,callouts,macros"]
  158. --------------------------------------------------
  159. include-tagged::{sql-specs}/docs/docs.csv-spec[dayOfMonth]
  160. --------------------------------------------------
  161. [[sql-functions-datetime-dow]]
  162. ==== `DAY_OF_WEEK/DAYOFWEEK/DOW`
  163. .Synopsis:
  164. [source, sql]
  165. --------------------------------------------------
  166. DAY_OF_WEEK(datetime_exp<1>)
  167. --------------------------------------------------
  168. *Input*:
  169. <1> date/datetime expression
  170. *Output*: integer
  171. .Description:
  172. Extract the day of the week from a date/datetime. Sunday is `1`, Monday is `2`, etc.
  173. ["source","sql",subs="attributes,callouts,macros"]
  174. --------------------------------------------------
  175. include-tagged::{sql-specs}/docs/docs.csv-spec[dayOfWeek]
  176. --------------------------------------------------
  177. [[sql-functions-datetime-doy]]
  178. ==== `DAY_OF_YEAR/DOY`
  179. .Synopsis:
  180. [source, sql]
  181. --------------------------------------------------
  182. DAY_OF_YEAR(datetime_exp<1>)
  183. --------------------------------------------------
  184. *Input*:
  185. <1> date/datetime expression
  186. *Output*: integer
  187. .Description:
  188. Extract the day of the year from a date/datetime.
  189. ["source","sql",subs="attributes,callouts,macros"]
  190. --------------------------------------------------
  191. include-tagged::{sql-specs}/docs/docs.csv-spec[dayOfYear]
  192. --------------------------------------------------
  193. [[sql-functions-datetime-dayname]]
  194. ==== `DAY_NAME/DAYNAME`
  195. .Synopsis:
  196. [source, sql]
  197. --------------------------------------------------
  198. DAY_NAME(datetime_exp<1>)
  199. --------------------------------------------------
  200. *Input*:
  201. <1> date/datetime expression
  202. *Output*: string
  203. .Description:
  204. Extract the day of the week from a date/datetime in text format (`Monday`, `Tuesday`...).
  205. ["source","sql",subs="attributes,callouts,macros"]
  206. --------------------------------------------------
  207. include-tagged::{sql-specs}/docs/docs.csv-spec[dayName]
  208. --------------------------------------------------
  209. [[sql-functions-datetime-hour]]
  210. ==== `HOUR_OF_DAY/HOUR`
  211. .Synopsis:
  212. [source, sql]
  213. --------------------------------------------------
  214. HOUR_OF_DAY(datetime_exp<1>)
  215. --------------------------------------------------
  216. *Input*:
  217. <1> date/datetime expression
  218. *Output*: integer
  219. .Description:
  220. Extract the hour of the day from a date/datetime.
  221. ["source","sql",subs="attributes,callouts,macros"]
  222. --------------------------------------------------
  223. include-tagged::{sql-specs}/docs/docs.csv-spec[hourOfDay]
  224. --------------------------------------------------
  225. [[sql-functions-datetime-isodow]]
  226. ==== `ISO_DAY_OF_WEEK/ISODAYOFWEEK/ISODOW/IDOW`
  227. .Synopsis:
  228. [source, sql]
  229. --------------------------------------------------
  230. ISO_DAY_OF_WEEK(datetime_exp<1>)
  231. --------------------------------------------------
  232. *Input*:
  233. <1> date/datetime expression
  234. *Output*: integer
  235. .Description:
  236. Extract the day of the week from a date/datetime, following the https://en.wikipedia.org/wiki/ISO_week_date[ISO 8601 standard].
  237. Monday is `1`, Tuesday is `2`, etc.
  238. ["source","sql",subs="attributes,callouts,macros"]
  239. --------------------------------------------------
  240. include-tagged::{sql-specs}/docs/docs.csv-spec[isoDayOfWeek]
  241. --------------------------------------------------
  242. [[sql-functions-datetime-isoweek]]
  243. ==== `ISO_WEEK_OF_YEAR/ISOWEEKOFYEAR/ISOWEEK/IWOY/IW`
  244. .Synopsis:
  245. [source, sql]
  246. --------------------------------------------------
  247. ISO_WEEK_OF_YEAR(datetime_exp<1>)
  248. --------------------------------------------------
  249. *Input*:
  250. <1> date/datetime expression
  251. *Output*: integer
  252. .Description:
  253. 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
  254. of a year is the first week with a majority (4 or more) of its days in January.
  255. ["source","sql",subs="attributes,callouts,macros"]
  256. --------------------------------------------------
  257. include-tagged::{sql-specs}/docs/docs.csv-spec[isoWeekOfYear]
  258. --------------------------------------------------
  259. [[sql-functions-datetime-minuteofday]]
  260. ==== `MINUTE_OF_DAY`
  261. .Synopsis:
  262. [source, sql]
  263. --------------------------------------------------
  264. MINUTE_OF_DAY(datetime_exp<1>)
  265. --------------------------------------------------
  266. *Input*:
  267. <1> date/datetime expression
  268. *Output*: integer
  269. .Description:
  270. Extract the minute of the day from a date/datetime.
  271. ["source","sql",subs="attributes,callouts,macros"]
  272. --------------------------------------------------
  273. include-tagged::{sql-specs}/docs/docs.csv-spec[minuteOfDay]
  274. --------------------------------------------------
  275. [[sql-functions-datetime-minute]]
  276. ==== `MINUTE_OF_HOUR/MINUTE`
  277. .Synopsis:
  278. [source, sql]
  279. --------------------------------------------------
  280. MINUTE_OF_HOUR(datetime_exp<1>)
  281. --------------------------------------------------
  282. *Input*:
  283. <1> date/datetime expression
  284. *Output*: integer
  285. .Description:
  286. Extract the minute of the hour from a date/datetime.
  287. ["source","sql",subs="attributes,callouts,macros"]
  288. --------------------------------------------------
  289. include-tagged::{sql-specs}/docs/docs.csv-spec[minuteOfHour]
  290. --------------------------------------------------
  291. [[sql-functions-datetime-month]]
  292. ==== `MONTH_OF_YEAR/MONTH`
  293. .Synopsis:
  294. [source, sql]
  295. --------------------------------------------------
  296. MONTH(datetime_exp<1>)
  297. --------------------------------------------------
  298. *Input*:
  299. <1> date/datetime expression
  300. *Output*: integer
  301. .Description:
  302. Extract the month of the year from a date/datetime.
  303. ["source","sql",subs="attributes,callouts,macros"]
  304. --------------------------------------------------
  305. include-tagged::{sql-specs}/docs/docs.csv-spec[monthOfYear]
  306. --------------------------------------------------
  307. [[sql-functions-datetime-monthname]]
  308. ==== `MONTH_NAME/MONTHNAME`
  309. .Synopsis:
  310. [source, sql]
  311. --------------------------------------------------
  312. MONTH_NAME(datetime_exp<1>)
  313. --------------------------------------------------
  314. *Input*:
  315. <1> date/datetime expression
  316. *Output*: string
  317. .Description:
  318. Extract the month from a date/datetime in text format (`January`, `February`...).
  319. ["source","sql",subs="attributes,callouts,macros"]
  320. --------------------------------------------------
  321. include-tagged::{sql-specs}/docs/docs.csv-spec[monthName]
  322. --------------------------------------------------
  323. [[sql-functions-now]]
  324. ==== `NOW`
  325. .Synopsis:
  326. [source, sql]
  327. --------------------------------------------------
  328. NOW()
  329. --------------------------------------------------
  330. *Input*: _none_
  331. *Output*: datetime
  332. .Description:
  333. This function offers the same functionality as <<sql-functions-current-timestamp,CURRENT_TIMESTAMP()>> function: returns
  334. the datetime when the current query reached the server. This method always returns the same value for its every
  335. occurrence within the same query.
  336. ["source","sql",subs="attributes,callouts,macros"]
  337. --------------------------------------------------
  338. include-tagged::{sql-specs}/docs/docs.csv-spec[nowFunction]
  339. --------------------------------------------------
  340. Typically, this function (as well as its twin <<sql-functions-current-timestamp,CURRENT_TIMESTAMP())>> function is used
  341. for relative date/time filtering:
  342. ["source","sql",subs="attributes,callouts,macros"]
  343. --------------------------------------------------
  344. include-tagged::{sql-specs}/docs/docs.csv-spec[filterNow]
  345. --------------------------------------------------
  346. [[sql-functions-datetime-second]]
  347. ==== `SECOND_OF_MINUTE/SECOND`
  348. .Synopsis:
  349. [source, sql]
  350. --------------------------------------------------
  351. SECOND_OF_MINUTE(datetime_exp<1>)
  352. --------------------------------------------------
  353. *Input*:
  354. <1> date/datetime expression
  355. *Output*: integer
  356. .Description:
  357. Extract the second of the minute from a date/datetime.
  358. ["source","sql",subs="attributes,callouts,macros"]
  359. --------------------------------------------------
  360. include-tagged::{sql-specs}/docs/docs.csv-spec[secondOfMinute]
  361. --------------------------------------------------
  362. [[sql-functions-datetime-quarter]]
  363. ==== `QUARTER`
  364. .Synopsis:
  365. [source, sql]
  366. --------------------------------------------------
  367. QUARTER(datetime_exp<1>)
  368. --------------------------------------------------
  369. *Input*:
  370. <1> date/datetime expression
  371. *Output*: integer
  372. .Description:
  373. Extract the year quarter the date/datetime falls in.
  374. ["source","sql",subs="attributes,callouts,macros"]
  375. --------------------------------------------------
  376. include-tagged::{sql-specs}/docs/docs.csv-spec[quarter]
  377. --------------------------------------------------
  378. [[sql-functions-today]]
  379. ==== `TODAY`
  380. .Synopsis:
  381. [source, sql]
  382. --------------------------------------------------
  383. TODAY()
  384. --------------------------------------------------
  385. *Input*: _none_
  386. *Output*: date
  387. .Description:
  388. This function offers the same functionality as <<sql-functions-current-date,CURRENT_DATE()>> function: returns
  389. the date when the current query reached the server. This method always returns the same value for its every occurrence
  390. within the same query.
  391. ["source","sql",subs="attributes,callouts,macros"]
  392. --------------------------------------------------
  393. include-tagged::{sql-specs}/docs/docs.csv-spec[todayFunction]
  394. --------------------------------------------------
  395. Typically, this function (as well as its twin <<sql-functions-current-timestamp,CURRENT_TIMESTAMP())>> function is used
  396. for relative date filtering:
  397. ["source","sql",subs="attributes,callouts,macros"]
  398. --------------------------------------------------
  399. include-tagged::{sql-specs}/docs/docs.csv-spec[filterToday]
  400. --------------------------------------------------
  401. [[sql-functions-datetime-week]]
  402. ==== `WEEK_OF_YEAR/WEEK`
  403. .Synopsis:
  404. [source, sql]
  405. --------------------------------------------------
  406. WEEK_OF_YEAR(datetime_exp<1>)
  407. --------------------------------------------------
  408. *Input*:
  409. <1> date/datetime expression
  410. *Output*: integer
  411. .Description:
  412. Extract the week of the year from a date/datetime.
  413. ["source","sql",subs="attributes,callouts,macros"]
  414. --------------------------------------------------
  415. include-tagged::{sql-specs}/docs/docs.csv-spec[weekOfYear]
  416. --------------------------------------------------
  417. [[sql-functions-datetime-year]]
  418. ==== `YEAR`
  419. .Synopsis:
  420. [source, sql]
  421. --------------------------------------------------
  422. YEAR(datetime_exp<1>)
  423. --------------------------------------------------
  424. *Input*:
  425. <1> date/datetime expression
  426. *Output*: integer
  427. .Description:
  428. Extract the year from a date/datetime.
  429. ["source","sql",subs="attributes,callouts,macros"]
  430. --------------------------------------------------
  431. include-tagged::{sql-specs}/docs/docs.csv-spec[year]
  432. --------------------------------------------------
  433. [[sql-functions-datetime-extract]]
  434. ==== `EXTRACT`
  435. .Synopsis:
  436. [source, sql]
  437. --------------------------------------------------
  438. EXTRACT(datetime_function<1> FROM datetime_exp<2>)
  439. --------------------------------------------------
  440. *Input*:
  441. <1> date/time function name
  442. <2> date/datetime expression
  443. *Output*: integer
  444. .Description:
  445. Extract fields from a date/datetime by specifying the name of a <<sql-functions-datetime,datetime function>>.
  446. The following
  447. ["source","sql",subs="attributes,callouts,macros"]
  448. --------------------------------------------------
  449. include-tagged::{sql-specs}/docs/docs.csv-spec[extractDayOfYear]
  450. --------------------------------------------------
  451. is the equivalent to
  452. ["source","sql",subs="attributes,callouts,macros"]
  453. --------------------------------------------------
  454. include-tagged::{sql-specs}/docs/docs.csv-spec[dayOfYear]
  455. --------------------------------------------------