date-time.asciidoc 15 KB

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