date-time.asciidoc 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641
  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.csv-spec[dtIntervalPlusInterval]
  49. --------------------------------------------------
  50. ["source","sql",subs="attributes,callouts,macros"]
  51. --------------------------------------------------
  52. include-tagged::{sql-specs}/docs.csv-spec[dtDateTimePlusInterval]
  53. --------------------------------------------------
  54. ["source","sql",subs="attributes,callouts,macros"]
  55. --------------------------------------------------
  56. include-tagged::{sql-specs}/docs.csv-spec[dtMinusInterval]
  57. --------------------------------------------------
  58. ["source","sql",subs="attributes,callouts,macros"]
  59. --------------------------------------------------
  60. include-tagged::{sql-specs}/docs.csv-spec[dtIntervalMinusInterval]
  61. --------------------------------------------------
  62. ["source","sql",subs="attributes,callouts,macros"]
  63. --------------------------------------------------
  64. include-tagged::{sql-specs}/docs.csv-spec[dtDateTimeMinusInterval]
  65. --------------------------------------------------
  66. ["source","sql",subs="attributes,callouts,macros"]
  67. --------------------------------------------------
  68. include-tagged::{sql-specs}/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. --------------------------------------------------
  80. *Input*: _none_
  81. *Output*: date
  82. .Description:
  83. Returns the date (no time part) when the current query reached the server.
  84. It can be used both as a keyword: `CURRENT_DATE` or as a function with no arguments: `CURRENT_DATE()`.
  85. [NOTE]
  86. Unlike CURRENT_DATE, `CURDATE()` can only be used as a function with no arguments and not as a keyword.
  87. This method always returns the same value for its every occurrence within the same query.
  88. ["source","sql",subs="attributes,callouts,macros"]
  89. --------------------------------------------------
  90. include-tagged::{sql-specs}/docs.csv-spec[curDate]
  91. --------------------------------------------------
  92. ["source","sql",subs="attributes,callouts,macros"]
  93. --------------------------------------------------
  94. include-tagged::{sql-specs}/docs.csv-spec[curDateFunction]
  95. --------------------------------------------------
  96. Typically, this function (as well as its twin <<sql-functions-today,TODAY())>> function
  97. is used for relative date filtering:
  98. ["source","sql",subs="attributes,callouts,macros"]
  99. --------------------------------------------------
  100. include-tagged::{sql-specs}/docs.csv-spec[filterToday]
  101. --------------------------------------------------
  102. [[sql-functions-current-timestamp]]
  103. ==== `CURRENT_TIMESTAMP`
  104. .Synopsis:
  105. [source, sql]
  106. --------------------------------------------------
  107. CURRENT_TIMESTAMP
  108. CURRENT_TIMESTAMP(precision <1>)
  109. --------------------------------------------------
  110. *Input*:
  111. <1> fractional digits; optional
  112. *Output*: date/time
  113. .Description:
  114. Returns the date/time when the current query reached the server.
  115. As a function, `CURRENT_TIMESTAMP()` accepts _precision_ as an optional
  116. parameter for rounding the second fractional digits (nanoseconds). The default _precision_ is 3,
  117. meaning a milliseconds precision current date/time will be returned.
  118. This method always returns the same value for its every occurrence within the same query.
  119. ["source","sql",subs="attributes,callouts,macros"]
  120. --------------------------------------------------
  121. include-tagged::{sql-specs}/docs.csv-spec[curTs]
  122. --------------------------------------------------
  123. ["source","sql",subs="attributes,callouts,macros"]
  124. --------------------------------------------------
  125. include-tagged::{sql-specs}/docs.csv-spec[curTsFunction]
  126. --------------------------------------------------
  127. ["source","sql",subs="attributes,callouts,macros"]
  128. --------------------------------------------------
  129. include-tagged::{sql-specs}/docs.csv-spec[curTsFunctionPrecision]
  130. --------------------------------------------------
  131. Typically, this function (as well as its twin <<sql-functions-now,NOW())>> function is used for
  132. relative date/time filtering:
  133. ["source","sql",subs="attributes,callouts,macros"]
  134. --------------------------------------------------
  135. include-tagged::{sql-specs}/docs.csv-spec[filterNow]
  136. --------------------------------------------------
  137. [[sql-functions-datetime-day]]
  138. ==== `DAY_OF_MONTH/DOM/DAY`
  139. .Synopsis:
  140. [source, sql]
  141. --------------------------------------------------
  142. DAY_OF_MONTH(datetime_exp<1>)
  143. --------------------------------------------------
  144. *Input*:
  145. <1> date/datetime expression
  146. *Output*: integer
  147. .Description:
  148. Extract the day of the month from a date/datetime.
  149. ["source","sql",subs="attributes,callouts,macros"]
  150. --------------------------------------------------
  151. include-tagged::{sql-specs}/docs.csv-spec[dayOfMonth]
  152. --------------------------------------------------
  153. [[sql-functions-datetime-dow]]
  154. ==== `DAY_OF_WEEK/DAYOFWEEK/DOW`
  155. .Synopsis:
  156. [source, sql]
  157. --------------------------------------------------
  158. DAY_OF_WEEK(datetime_exp<1>)
  159. --------------------------------------------------
  160. *Input*:
  161. <1> date/datetime expression
  162. *Output*: integer
  163. .Description:
  164. Extract the day of the week from a date/datetime. Sunday is `1`, Monday is `2`, etc.
  165. ["source","sql",subs="attributes,callouts,macros"]
  166. --------------------------------------------------
  167. include-tagged::{sql-specs}/docs.csv-spec[dayOfWeek]
  168. --------------------------------------------------
  169. [[sql-functions-datetime-doy]]
  170. ==== `DAY_OF_YEAR/DOY`
  171. .Synopsis:
  172. [source, sql]
  173. --------------------------------------------------
  174. DAY_OF_YEAR(datetime_exp<1>)
  175. --------------------------------------------------
  176. *Input*:
  177. <1> date/datetime expression
  178. *Output*: integer
  179. .Description:
  180. Extract the day of the year from a date/datetime.
  181. ["source","sql",subs="attributes,callouts,macros"]
  182. --------------------------------------------------
  183. include-tagged::{sql-specs}/docs.csv-spec[dayOfYear]
  184. --------------------------------------------------
  185. [[sql-functions-datetime-dayname]]
  186. ==== `DAY_NAME/DAYNAME`
  187. .Synopsis:
  188. [source, sql]
  189. --------------------------------------------------
  190. DAY_NAME(datetime_exp<1>)
  191. --------------------------------------------------
  192. *Input*:
  193. <1> date/datetime expression
  194. *Output*: string
  195. .Description:
  196. Extract the day of the week from a date/datetime in text format (`Monday`, `Tuesday`...).
  197. ["source","sql",subs="attributes,callouts,macros"]
  198. --------------------------------------------------
  199. include-tagged::{sql-specs}/docs.csv-spec[dayName]
  200. --------------------------------------------------
  201. [[sql-functions-datetime-hour]]
  202. ==== `HOUR_OF_DAY/HOUR`
  203. .Synopsis:
  204. [source, sql]
  205. --------------------------------------------------
  206. HOUR_OF_DAY(datetime_exp<1>)
  207. --------------------------------------------------
  208. *Input*:
  209. <1> date/datetime expression
  210. *Output*: integer
  211. .Description:
  212. Extract the hour of the day from a date/datetime.
  213. ["source","sql",subs="attributes,callouts,macros"]
  214. --------------------------------------------------
  215. include-tagged::{sql-specs}/docs.csv-spec[hourOfDay]
  216. --------------------------------------------------
  217. [[sql-functions-datetime-isodow]]
  218. ==== `ISO_DAY_OF_WEEK/ISODAYOFWEEK/ISODOW/IDOW`
  219. .Synopsis:
  220. [source, sql]
  221. --------------------------------------------------
  222. ISO_DAY_OF_WEEK(datetime_exp<1>)
  223. --------------------------------------------------
  224. *Input*:
  225. <1> date/datetime expression
  226. *Output*: integer
  227. .Description:
  228. Extract the day of the week from a date/datetime, following the https://en.wikipedia.org/wiki/ISO_week_date[ISO 8601 standard].
  229. Monday is `1`, Tuesday is `2`, etc.
  230. ["source","sql",subs="attributes,callouts,macros"]
  231. --------------------------------------------------
  232. include-tagged::{sql-specs}/docs.csv-spec[isoDayOfWeek]
  233. --------------------------------------------------
  234. [[sql-functions-datetime-isoweek]]
  235. ==== `ISO_WEEK_OF_YEAR/ISOWEEKOFYEAR/ISOWEEK/IWOY/IW`
  236. .Synopsis:
  237. [source, sql]
  238. --------------------------------------------------
  239. ISO_WEEK_OF_YEAR(datetime_exp<1>)
  240. --------------------------------------------------
  241. *Input*:
  242. <1> date/datetime expression
  243. *Output*: integer
  244. .Description:
  245. 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
  246. of a year is the first week with a majority (4 or more) of its days in January.
  247. ["source","sql",subs="attributes,callouts,macros"]
  248. --------------------------------------------------
  249. include-tagged::{sql-specs}/docs.csv-spec[isoWeekOfYear]
  250. --------------------------------------------------
  251. [[sql-functions-datetime-minuteofday]]
  252. ==== `MINUTE_OF_DAY`
  253. .Synopsis:
  254. [source, sql]
  255. --------------------------------------------------
  256. MINUTE_OF_DAY(datetime_exp<1>)
  257. --------------------------------------------------
  258. *Input*:
  259. <1> date/datetime expression
  260. *Output*: integer
  261. .Description:
  262. Extract the minute of the day from a date/datetime.
  263. ["source","sql",subs="attributes,callouts,macros"]
  264. --------------------------------------------------
  265. include-tagged::{sql-specs}/docs.csv-spec[minuteOfDay]
  266. --------------------------------------------------
  267. [[sql-functions-datetime-minute]]
  268. ==== `MINUTE_OF_HOUR/MINUTE`
  269. .Synopsis:
  270. [source, sql]
  271. --------------------------------------------------
  272. MINUTE_OF_HOUR(datetime_exp<1>)
  273. --------------------------------------------------
  274. *Input*:
  275. <1> date/datetime expression
  276. *Output*: integer
  277. .Description:
  278. Extract the minute of the hour from a date/datetime.
  279. ["source","sql",subs="attributes,callouts,macros"]
  280. --------------------------------------------------
  281. include-tagged::{sql-specs}/docs.csv-spec[minuteOfHour]
  282. --------------------------------------------------
  283. [[sql-functions-datetime-month]]
  284. ==== `MONTH_OF_YEAR/MONTH`
  285. .Synopsis:
  286. [source, sql]
  287. --------------------------------------------------
  288. MONTH(datetime_exp<1>)
  289. --------------------------------------------------
  290. *Input*:
  291. <1> date/datetime expression
  292. *Output*: integer
  293. .Description:
  294. Extract the month of the year from a date/datetime.
  295. ["source","sql",subs="attributes,callouts,macros"]
  296. --------------------------------------------------
  297. include-tagged::{sql-specs}/docs.csv-spec[monthOfYear]
  298. --------------------------------------------------
  299. [[sql-functions-datetime-monthname]]
  300. ==== `MONTH_NAME/MONTHNAME`
  301. .Synopsis:
  302. [source, sql]
  303. --------------------------------------------------
  304. MONTH_NAME(datetime_exp<1>)
  305. --------------------------------------------------
  306. *Input*:
  307. <1> date/datetime expression
  308. *Output*: string
  309. .Description:
  310. Extract the month from a date/datetime in text format (`January`, `February`...).
  311. ["source","sql",subs="attributes,callouts,macros"]
  312. --------------------------------------------------
  313. include-tagged::{sql-specs}/docs.csv-spec[monthName]
  314. --------------------------------------------------
  315. [[sql-functions-now]]
  316. ==== `NOW`
  317. .Synopsis:
  318. [source, sql]
  319. --------------------------------------------------
  320. NOW()
  321. --------------------------------------------------
  322. *Input*: _none_
  323. *Output*: datetime
  324. .Description:
  325. This function offers the same functionality as <<sql-functions-current-timestamp,CURRENT_TIMESTAMP()>> function: returns
  326. the datetime when the current query reached the server. This method always returns the same value for its every
  327. occurrence within the same query.
  328. ["source","sql",subs="attributes,callouts,macros"]
  329. --------------------------------------------------
  330. include-tagged::{sql-specs}/docs.csv-spec[nowFunction]
  331. --------------------------------------------------
  332. Typically, this function (as well as its twin <<sql-functions-current-timestamp,CURRENT_TIMESTAMP())>> function is used
  333. for relative date/time filtering:
  334. ["source","sql",subs="attributes,callouts,macros"]
  335. --------------------------------------------------
  336. include-tagged::{sql-specs}/docs.csv-spec[filterNow]
  337. --------------------------------------------------
  338. [[sql-functions-datetime-second]]
  339. ==== `SECOND_OF_MINUTE/SECOND`
  340. .Synopsis:
  341. [source, sql]
  342. --------------------------------------------------
  343. SECOND_OF_MINUTE(datetime_exp<1>)
  344. --------------------------------------------------
  345. *Input*:
  346. <1> date/datetime expression
  347. *Output*: integer
  348. .Description:
  349. Extract the second of the minute from a date/datetime.
  350. ["source","sql",subs="attributes,callouts,macros"]
  351. --------------------------------------------------
  352. include-tagged::{sql-specs}/docs.csv-spec[secondOfMinute]
  353. --------------------------------------------------
  354. [[sql-functions-datetime-quarter]]
  355. ==== `QUARTER`
  356. .Synopsis:
  357. [source, sql]
  358. --------------------------------------------------
  359. QUARTER(datetime_exp<1>)
  360. --------------------------------------------------
  361. *Input*:
  362. <1> date/datetime expression
  363. *Output*: integer
  364. .Description:
  365. Extract the year quarter the date/datetime falls in.
  366. ["source","sql",subs="attributes,callouts,macros"]
  367. --------------------------------------------------
  368. include-tagged::{sql-specs}/docs.csv-spec[quarter]
  369. --------------------------------------------------
  370. [[sql-functions-today]]
  371. ==== `TODAY`
  372. .Synopsis:
  373. [source, sql]
  374. --------------------------------------------------
  375. TODAY()
  376. --------------------------------------------------
  377. *Input*: _none_
  378. *Output*: date
  379. .Description:
  380. This function offers the same functionality as <<sql-functions-current-date,CURRENT_DATE()>> function: returns
  381. the date when the current query reached the server. This method always returns the same value for its every occurrence
  382. within the same query.
  383. ["source","sql",subs="attributes,callouts,macros"]
  384. --------------------------------------------------
  385. include-tagged::{sql-specs}/docs.csv-spec[todayFunction]
  386. --------------------------------------------------
  387. Typically, this function (as well as its twin <<sql-functions-current-timestamp,CURRENT_TIMESTAMP())>> function is used
  388. for relative date filtering:
  389. ["source","sql",subs="attributes,callouts,macros"]
  390. --------------------------------------------------
  391. include-tagged::{sql-specs}/docs.csv-spec[filterToday]
  392. --------------------------------------------------
  393. [[sql-functions-datetime-week]]
  394. ==== `WEEK_OF_YEAR/WEEK`
  395. .Synopsis:
  396. [source, sql]
  397. --------------------------------------------------
  398. WEEK_OF_YEAR(datetime_exp<1>)
  399. --------------------------------------------------
  400. *Input*:
  401. <1> date/datetime expression
  402. *Output*: integer
  403. .Description:
  404. Extract the week of the year from a date/datetime.
  405. ["source","sql",subs="attributes,callouts,macros"]
  406. --------------------------------------------------
  407. include-tagged::{sql-specs}/docs.csv-spec[weekOfYear]
  408. --------------------------------------------------
  409. [[sql-functions-datetime-year]]
  410. ==== `YEAR`
  411. .Synopsis:
  412. [source, sql]
  413. --------------------------------------------------
  414. YEAR(datetime_exp<1>)
  415. --------------------------------------------------
  416. *Input*:
  417. <1> date/datetime expression
  418. *Output*: integer
  419. .Description:
  420. Extract the year from a date/datetime.
  421. ["source","sql",subs="attributes,callouts,macros"]
  422. --------------------------------------------------
  423. include-tagged::{sql-specs}/docs.csv-spec[year]
  424. --------------------------------------------------
  425. [[sql-functions-datetime-extract]]
  426. ==== `EXTRACT`
  427. .Synopsis:
  428. [source, sql]
  429. --------------------------------------------------
  430. EXTRACT(datetime_function<1> FROM datetime_exp<2>)
  431. --------------------------------------------------
  432. *Input*:
  433. <1> date/time function name
  434. <2> date/datetime expression
  435. *Output*: integer
  436. .Description:
  437. Extract fields from a date/datetime by specifying the name of a <<sql-functions-datetime,datetime function>>.
  438. The following
  439. ["source","sql",subs="attributes,callouts,macros"]
  440. --------------------------------------------------
  441. include-tagged::{sql-specs}/docs.csv-spec[extractDayOfYear]
  442. --------------------------------------------------
  443. is the equivalent to
  444. ["source","sql",subs="attributes,callouts,macros"]
  445. --------------------------------------------------
  446. include-tagged::{sql-specs}/docs.csv-spec[dayOfYear]
  447. --------------------------------------------------