date-time.asciidoc 14 KB

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