date-time.asciidoc 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047
  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 (`+`, `-`, `*`) support date/time parameters as indicated below:
  46. [source, sql]
  47. --------------------------------------------------
  48. include-tagged::{sql-specs}/docs/docs.csv-spec[dtIntervalPlusInterval]
  49. --------------------------------------------------
  50. [source, sql]
  51. --------------------------------------------------
  52. include-tagged::{sql-specs}/docs/docs.csv-spec[dtDateTimePlusInterval]
  53. --------------------------------------------------
  54. [source, sql]
  55. --------------------------------------------------
  56. include-tagged::{sql-specs}/docs/docs.csv-spec[dtMinusInterval]
  57. --------------------------------------------------
  58. [source, sql]
  59. --------------------------------------------------
  60. include-tagged::{sql-specs}/docs/docs.csv-spec[dtIntervalMinusInterval]
  61. --------------------------------------------------
  62. [source, sql]
  63. --------------------------------------------------
  64. include-tagged::{sql-specs}/docs/docs.csv-spec[dtDateTimeMinusInterval]
  65. --------------------------------------------------
  66. [source, sql]
  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*: 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]
  89. --------------------------------------------------
  90. include-tagged::{sql-specs}/docs/docs.csv-spec[currentDate]
  91. --------------------------------------------------
  92. [source, sql]
  93. --------------------------------------------------
  94. include-tagged::{sql-specs}/docs/docs.csv-spec[currentDateFunction]
  95. --------------------------------------------------
  96. [source, sql]
  97. --------------------------------------------------
  98. include-tagged::{sql-specs}/docs/docs.csv-spec[curDateFunction]
  99. --------------------------------------------------
  100. Typically, this function (as well as its twin <<sql-functions-today,TODAY())>> function
  101. is used for relative date filtering:
  102. [source, sql]
  103. --------------------------------------------------
  104. include-tagged::{sql-specs}/docs/docs.csv-spec[filterToday]
  105. --------------------------------------------------
  106. [[sql-functions-current-time]]
  107. ==== `CURRENT_TIME/CURTIME`
  108. .Synopsis:
  109. [source, sql]
  110. --------------------------------------------------
  111. CURRENT_TIME
  112. CURRENT_TIME([precision]) <1>
  113. CURTIME
  114. --------------------------------------------------
  115. *Input*:
  116. <1> fractional digits; optional
  117. *Output*: time
  118. *Description*: Returns the time when the current query reached the server.
  119. As a function, `CURRENT_TIME()` accepts _precision_ as an optional
  120. parameter for rounding the second fractional digits (nanoseconds). The default _precision_ is 3,
  121. meaning a milliseconds precision current time will be returned.
  122. This method always returns the same value for its every occurrence within the same query.
  123. [source, sql]
  124. --------------------------------------------------
  125. include-tagged::{sql-specs}/docs/docs.csv-spec[currentTime]
  126. --------------------------------------------------
  127. [source, sql]
  128. --------------------------------------------------
  129. include-tagged::{sql-specs}/docs/docs.csv-spec[currentTimeFunction]
  130. --------------------------------------------------
  131. [source, sql]
  132. --------------------------------------------------
  133. include-tagged::{sql-specs}/docs/docs.csv-spec[curTimeFunction]
  134. --------------------------------------------------
  135. [source, sql]
  136. --------------------------------------------------
  137. include-tagged::{sql-specs}/docs/docs.csv-spec[currentTimeFunctionPrecision]
  138. --------------------------------------------------
  139. Typically, this function is used for relative date/time filtering:
  140. [source, sql]
  141. --------------------------------------------------
  142. include-tagged::{sql-specs}/docs/docs.csv-spec[filterCurrentTime]
  143. --------------------------------------------------
  144. [IMPORTANT]
  145. Currently, using a _precision_ greater than 3 doesn't make any difference to the output of the
  146. function as the maximum number of second fractional digits returned is 3 (milliseconds).
  147. [[sql-functions-current-timestamp]]
  148. ==== `CURRENT_TIMESTAMP`
  149. .Synopsis:
  150. [source, sql]
  151. --------------------------------------------------
  152. CURRENT_TIMESTAMP
  153. CURRENT_TIMESTAMP([precision]) <1>
  154. --------------------------------------------------
  155. *Input*:
  156. <1> fractional digits; optional
  157. *Output*: date/time
  158. *Description*: Returns the date/time when the current query reached the server.
  159. As a function, `CURRENT_TIMESTAMP()` accepts _precision_ as an optional
  160. parameter for rounding the second fractional digits (nanoseconds). The default _precision_ is 3,
  161. meaning a milliseconds precision current date/time will be returned.
  162. This method always returns the same value for its every occurrence within the same query.
  163. [source, sql]
  164. --------------------------------------------------
  165. include-tagged::{sql-specs}/docs/docs.csv-spec[curTs]
  166. --------------------------------------------------
  167. [source, sql]
  168. --------------------------------------------------
  169. include-tagged::{sql-specs}/docs/docs.csv-spec[curTsFunction]
  170. --------------------------------------------------
  171. [source, sql]
  172. --------------------------------------------------
  173. include-tagged::{sql-specs}/docs/docs.csv-spec[curTsFunctionPrecision]
  174. --------------------------------------------------
  175. Typically, this function (as well as its twin <<sql-functions-now,NOW())>> function is used for
  176. relative date/time filtering:
  177. [source, sql]
  178. --------------------------------------------------
  179. include-tagged::{sql-specs}/docs/docs.csv-spec[filterNow]
  180. --------------------------------------------------
  181. [IMPORTANT]
  182. Currently, using a _precision_ greater than 3 doesn't make any difference to the output of the
  183. function as the maximum number of second fractional digits returned is 3 (milliseconds).
  184. [[sql-functions-datetime-add]]
  185. ==== `DATE_ADD/DATEADD/TIMESTAMP_ADD/TIMESTAMPADD`
  186. .Synopsis:
  187. [source, sql]
  188. --------------------------------------------------
  189. DATE_ADD(
  190. string_exp, <1>
  191. integer_exp, <2>
  192. datetime_exp) <3>
  193. --------------------------------------------------
  194. *Input*:
  195. <1> string expression denoting the date/time unit to add to the date/datetime
  196. <2> integer expression denoting how many times the above unit should be added to/from the date/datetime,
  197. if a negative value is used it results to a subtraction from the date/datetime
  198. <3> date/datetime expression
  199. *Output*: datetime
  200. *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
  201. the date/datetime. If any of the three arguments is `null` a `null` is returned.
  202. [WARNING]
  203. If the second argument is a long there is possibility of truncation since an integer value will be extracted and
  204. used from that long.
  205. [cols="^,^"]
  206. |===
  207. 2+h|Datetime units to add/subtract
  208. s|unit
  209. s|abbreviations
  210. | year | years, yy, yyyy
  211. | quarter | quarters, qq, q
  212. | month | months, mm, m
  213. | dayofyear | dy, y
  214. | day | days, dd, d
  215. | week | weeks, wk, ww
  216. | weekday | weekdays, dw
  217. | hour | hours, hh
  218. | minute | minutes, mi, n
  219. | second | seconds, ss, s
  220. | millisecond | milliseconds, ms
  221. | microsecond | microseconds, mcs
  222. | nanosecond | nanoseconds, ns
  223. |===
  224. [source, sql]
  225. --------------------------------------------------
  226. include-tagged::{sql-specs}/docs/docs.csv-spec[dateAddDateTimeYears]
  227. --------------------------------------------------
  228. [source, sql]
  229. --------------------------------------------------
  230. include-tagged::{sql-specs}/docs/docs.csv-spec[dateAddDateTimeWeeks]
  231. --------------------------------------------------
  232. [source, sql]
  233. --------------------------------------------------
  234. include-tagged::{sql-specs}/docs/docs.csv-spec[dateAddDateTimeSeconds]
  235. --------------------------------------------------
  236. [source, sql]
  237. --------------------------------------------------
  238. include-tagged::{sql-specs}/docs/docs.csv-spec[dateAddDateQuarters]
  239. --------------------------------------------------
  240. [source, sql]
  241. --------------------------------------------------
  242. include-tagged::{sql-specs}/docs/docs.csv-spec[dateAddDateMinutes]
  243. --------------------------------------------------
  244. [[sql-functions-datetime-diff]]
  245. ==== `DATE_DIFF/DATEDIFF/TIMESTAMP_DIFF/TIMESTAMPDIFF`
  246. .Synopsis:
  247. [source, sql]
  248. --------------------------------------------------
  249. DATE_DIFF(
  250. string_exp, <1>
  251. datetime_exp, <2>
  252. datetime_exp) <3>
  253. --------------------------------------------------
  254. *Input*:
  255. <1> string expression denoting the date/time unit difference between the following two date/datetime expressions
  256. <2> start date/datetime expression
  257. <3> end date/datetime expression
  258. *Output*: integer
  259. *Description*: Subtract the second argument from the third argument and return their difference in multiples of the unit
  260. specified in the first argument. If the second argument (start) is greater than the third argument (end),
  261. then negative values are returned. If any of the three arguments is `null`, a `null` is returned.
  262. [cols="^,^"]
  263. |===
  264. 2+h|Datetime difference units
  265. s|unit
  266. s|abbreviations
  267. | year | years, yy, yyyy
  268. | quarter | quarters, qq, q
  269. | month | months, mm, m
  270. | dayofyear | dy, y
  271. | day | days, dd, d
  272. | week | weeks, wk, ww
  273. | weekday | weekdays, dw
  274. | hour | hours, hh
  275. | minute | minutes, mi, n
  276. | second | seconds, ss, s
  277. | millisecond | milliseconds, ms
  278. | microsecond | microseconds, mcs
  279. | nanosecond | nanoseconds, ns
  280. |===
  281. [source, sql]
  282. --------------------------------------------------
  283. include-tagged::{sql-specs}/docs/docs.csv-spec[dateDiffDateTimeYears]
  284. --------------------------------------------------
  285. [source, sql]
  286. --------------------------------------------------
  287. include-tagged::{sql-specs}/docs/docs.csv-spec[dateDiffDateTimeWeeks]
  288. --------------------------------------------------
  289. [source, sql]
  290. --------------------------------------------------
  291. include-tagged::{sql-specs}/docs/docs.csv-spec[dateDiffDateTimeSeconds]
  292. --------------------------------------------------
  293. [source, sql]
  294. --------------------------------------------------
  295. include-tagged::{sql-specs}/docs/docs.csv-spec[dateDiffDateQuarters]
  296. --------------------------------------------------
  297. [NOTE]
  298. For `hour` and `minute`, `DATEDIFF` doesn't do any rounding, but instead first truncates
  299. the more detailed time fields on the 2 dates to zero and then calculates the subtraction.
  300. [source, sql]
  301. --------------------------------------------------
  302. include-tagged::{sql-specs}/docs/docs.csv-spec[dateDiffDateTimeHours]
  303. --------------------------------------------------
  304. [source, sql]
  305. --------------------------------------------------
  306. include-tagged::{sql-specs}/docs/docs.csv-spec[dateDiffDateTimeMinutes]
  307. --------------------------------------------------
  308. [source, sql]
  309. --------------------------------------------------
  310. include-tagged::{sql-specs}/docs/docs.csv-spec[dateDiffDateMinutes]
  311. --------------------------------------------------
  312. [[sql-functions-datetime-datetimeformat]]
  313. ==== `DATETIME_FORMAT`
  314. .Synopsis:
  315. [source, sql]
  316. --------------------------------------------------
  317. DATETIME_FORMAT(
  318. date_exp/datetime_exp/time_exp, <1>
  319. string_exp) <2>
  320. --------------------------------------------------
  321. *Input*:
  322. <1> date/datetime/time expression
  323. <2> format pattern
  324. *Output*: string
  325. *Description*: Returns the date/datetime/time as a string using the format specified in the 2nd argument. The formatting
  326. pattern used is the one from
  327. https://docs.oracle.com/en/java/javase/14/docs/api/java.base/java/time/format/DateTimeFormatter.html[`java.time.format.DateTimeFormatter`].
  328. If any of the two arguments is `null` or the pattern is an empty string `null` is returned.
  329. NOTE::
  330. If the 1st argument is of type `time`, then pattern specified by the 2nd argument cannot contain date related units
  331. (e.g. 'dd', 'MM', 'YYYY', etc.). If it contains such units an error is returned.
  332. [source, sql]
  333. --------------------------------------------------
  334. include-tagged::{sql-specs}/docs/docs.csv-spec[dateTimeFormatDate]
  335. --------------------------------------------------
  336. [source, sql]
  337. --------------------------------------------------
  338. include-tagged::{sql-specs}/docs/docs.csv-spec[dateTimeFormatDateTime]
  339. --------------------------------------------------
  340. [source, sql]
  341. --------------------------------------------------
  342. include-tagged::{sql-specs}/docs/docs.csv-spec[dateTimeFormatTime]
  343. --------------------------------------------------
  344. [[sql-functions-datetime-part]]
  345. ==== `DATE_PART/DATEPART`
  346. .Synopsis:
  347. [source, sql]
  348. --------------------------------------------------
  349. DATE_PART(
  350. string_exp, <1>
  351. datetime_exp) <2>
  352. --------------------------------------------------
  353. *Input*:
  354. <1> string expression denoting the unit to extract from the date/datetime
  355. <2> date/datetime expression
  356. *Output*: integer
  357. *Description*: Extract the specified unit from a date/datetime. If any of the two arguments is `null` a `null` is returned.
  358. It's similar to <<sql-functions-datetime-extract>> but with different names and aliases for the units and
  359. provides more options (e.g.: `TZOFFSET`).
  360. [cols="^,^"]
  361. |===
  362. 2+h|Datetime units to extract
  363. s|unit
  364. s|abbreviations
  365. | year | years, yy, yyyy
  366. | quarter | quarters, qq, q
  367. | month | months, mm, m
  368. | dayofyear | dy, y
  369. | day | days, dd, d
  370. | week | weeks, wk, ww
  371. | weekday | weekdays, dw
  372. | hour | hours, hh
  373. | minute | minutes, mi, n
  374. | second | seconds, ss, s
  375. | millisecond | milliseconds, ms
  376. | microsecond | microseconds, mcs
  377. | nanosecond | nanoseconds, ns
  378. | tzoffset | tz
  379. |===
  380. [source, sql]
  381. --------------------------------------------------
  382. include-tagged::{sql-specs}/docs/docs.csv-spec[datePartDateTimeYears]
  383. --------------------------------------------------
  384. [source, sql]
  385. --------------------------------------------------
  386. include-tagged::{sql-specs}/docs/docs.csv-spec[datePartDateTimeMinutes]
  387. --------------------------------------------------
  388. [source, sql]
  389. --------------------------------------------------
  390. include-tagged::{sql-specs}/docs/docs.csv-spec[datePartDateQuarter]
  391. --------------------------------------------------
  392. [source, sql]
  393. --------------------------------------------------
  394. include-tagged::{sql-specs}/docs/docs.csv-spec[datePartDateMonth]
  395. --------------------------------------------------
  396. [NOTE]
  397. For `week` and `weekday` the unit is extracted using the non-ISO calculation, which means
  398. that a given week is considered to start from Sunday, not Monday.
  399. [source, sql]
  400. --------------------------------------------------
  401. include-tagged::{sql-specs}/docs/docs.csv-spec[datePartDateTimeWeek]
  402. --------------------------------------------------
  403. [NOTE]
  404. The `tzoffset` returns the total number of minutes (signed) that represent the time zone's offset.
  405. [source, sql]
  406. --------------------------------------------------
  407. include-tagged::{sql-specs}/docs/docs.csv-spec[datePartDateTimeTzOffsetPlus]
  408. --------------------------------------------------
  409. [source, sql]
  410. --------------------------------------------------
  411. include-tagged::{sql-specs}/docs/docs.csv-spec[datePartDateTimeTzOffsetMinus]
  412. --------------------------------------------------
  413. [[sql-functions-datetime-trunc]]
  414. ==== `DATE_TRUNC/DATETRUNC`
  415. .Synopsis:
  416. [source, sql]
  417. --------------------------------------------------
  418. DATE_TRUNC(
  419. string_exp, <1>
  420. datetime_exp/interval_exp) <2>
  421. --------------------------------------------------
  422. *Input*:
  423. <1> string expression denoting the unit to which the date/datetime/interval should be truncated to
  424. <2> date/datetime/interval expression
  425. *Output*: datetime/interval
  426. *Description*: Truncate the date/datetime/interval to the specified unit by setting all fields that are less significant than the specified
  427. one to zero (or one, for day, day of week and month). If any of the two arguments is `null` a `null` is returned.
  428. 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.
  429. [cols="^,^"]
  430. |===
  431. 2+h|Datetime truncation units
  432. s|unit
  433. s|abbreviations
  434. | millennium | millennia
  435. | century | centuries
  436. | decade | decades
  437. | year | years, yy, yyyy
  438. | quarter | quarters, qq, q
  439. | month | months, mm, m
  440. | week | weeks, wk, ww
  441. | day | days, dd, d
  442. | hour | hours, hh
  443. | minute | minutes, mi, n
  444. | second | seconds, ss, s
  445. | millisecond | milliseconds, ms
  446. | microsecond | microseconds, mcs
  447. | nanosecond | nanoseconds, ns
  448. |===
  449. [source, sql]
  450. --------------------------------------------------
  451. include-tagged::{sql-specs}/docs/docs.csv-spec[truncateDateTimeMillennium]
  452. --------------------------------------------------
  453. [source, sql]
  454. --------------------------------------------------
  455. include-tagged::{sql-specs}/docs/docs.csv-spec[truncateDateTimeWeek]
  456. --------------------------------------------------
  457. [source, sql]
  458. --------------------------------------------------
  459. include-tagged::{sql-specs}/docs/docs.csv-spec[truncateDateTimeMinutes]
  460. --------------------------------------------------
  461. [source, sql]
  462. --------------------------------------------------
  463. include-tagged::{sql-specs}/docs/docs.csv-spec[truncateDateDecades]
  464. --------------------------------------------------
  465. [source, sql]
  466. --------------------------------------------------
  467. include-tagged::{sql-specs}/docs/docs.csv-spec[truncateDateQuarter]
  468. --------------------------------------------------
  469. [source, sql]
  470. --------------------------------------------------
  471. include-tagged::{sql-specs}/docs/docs.csv-spec[truncateIntervalCenturies]
  472. --------------------------------------------------
  473. [source, sql]
  474. --------------------------------------------------
  475. include-tagged::{sql-specs}/docs/docs.csv-spec[truncateIntervalHour]
  476. --------------------------------------------------
  477. [source, sql]
  478. --------------------------------------------------
  479. include-tagged::{sql-specs}/docs/docs.csv-spec[truncateIntervalDay]
  480. --------------------------------------------------
  481. [[sql-functions-datetime-day]]
  482. ==== `DAY_OF_MONTH/DOM/DAY`
  483. .Synopsis:
  484. [source, sql]
  485. --------------------------------------------------
  486. DAY_OF_MONTH(datetime_exp) <1>
  487. --------------------------------------------------
  488. *Input*:
  489. <1> date/datetime expression
  490. *Output*: integer
  491. *Description*: Extract the day of the month from a date/datetime.
  492. [source, sql]
  493. --------------------------------------------------
  494. include-tagged::{sql-specs}/docs/docs.csv-spec[dayOfMonth]
  495. --------------------------------------------------
  496. [[sql-functions-datetime-dow]]
  497. ==== `DAY_OF_WEEK/DAYOFWEEK/DOW`
  498. .Synopsis:
  499. [source, sql]
  500. --------------------------------------------------
  501. DAY_OF_WEEK(datetime_exp) <1>
  502. --------------------------------------------------
  503. *Input*:
  504. <1> date/datetime expression
  505. *Output*: integer
  506. *Description*: Extract the day of the week from a date/datetime. Sunday is `1`, Monday is `2`, etc.
  507. [source, sql]
  508. --------------------------------------------------
  509. include-tagged::{sql-specs}/docs/docs.csv-spec[dayOfWeek]
  510. --------------------------------------------------
  511. [[sql-functions-datetime-doy]]
  512. ==== `DAY_OF_YEAR/DOY`
  513. .Synopsis:
  514. [source, sql]
  515. --------------------------------------------------
  516. DAY_OF_YEAR(datetime_exp) <1>
  517. --------------------------------------------------
  518. *Input*:
  519. <1> date/datetime expression
  520. *Output*: integer
  521. *Description*: Extract the day of the year from a date/datetime.
  522. [source, sql]
  523. --------------------------------------------------
  524. include-tagged::{sql-specs}/docs/docs.csv-spec[dayOfYear]
  525. --------------------------------------------------
  526. [[sql-functions-datetime-dayname]]
  527. ==== `DAY_NAME/DAYNAME`
  528. .Synopsis:
  529. [source, sql]
  530. --------------------------------------------------
  531. DAY_NAME(datetime_exp) <1>
  532. --------------------------------------------------
  533. *Input*:
  534. <1> date/datetime expression
  535. *Output*: string
  536. *Description*: Extract the day of the week from a date/datetime in text format (`Monday`, `Tuesday`...).
  537. [source, sql]
  538. --------------------------------------------------
  539. include-tagged::{sql-specs}/docs/docs.csv-spec[dayName]
  540. --------------------------------------------------
  541. [[sql-functions-datetime-hour]]
  542. ==== `HOUR_OF_DAY/HOUR`
  543. .Synopsis:
  544. [source, sql]
  545. --------------------------------------------------
  546. HOUR_OF_DAY(datetime_exp) <1>
  547. --------------------------------------------------
  548. *Input*:
  549. <1> date/datetime expression
  550. *Output*: integer
  551. *Description*: Extract the hour of the day from a date/datetime.
  552. [source, sql]
  553. --------------------------------------------------
  554. include-tagged::{sql-specs}/docs/docs.csv-spec[hourOfDay]
  555. --------------------------------------------------
  556. [[sql-functions-datetime-isodow]]
  557. ==== `ISO_DAY_OF_WEEK/ISODAYOFWEEK/ISODOW/IDOW`
  558. .Synopsis:
  559. [source, sql]
  560. --------------------------------------------------
  561. ISO_DAY_OF_WEEK(datetime_exp) <1>
  562. --------------------------------------------------
  563. *Input*:
  564. <1> date/datetime expression
  565. *Output*: integer
  566. *Description*: Extract the day of the week from a date/datetime, following the https://en.wikipedia.org/wiki/ISO_week_date[ISO 8601 standard].
  567. Monday is `1`, Tuesday is `2`, etc.
  568. [source, sql]
  569. --------------------------------------------------
  570. include-tagged::{sql-specs}/docs/docs.csv-spec[isoDayOfWeek]
  571. --------------------------------------------------
  572. [[sql-functions-datetime-isoweek]]
  573. ==== `ISO_WEEK_OF_YEAR/ISOWEEKOFYEAR/ISOWEEK/IWOY/IW`
  574. .Synopsis:
  575. [source, sql]
  576. --------------------------------------------------
  577. ISO_WEEK_OF_YEAR(datetime_exp) <1>
  578. --------------------------------------------------
  579. *Input*:
  580. <1> date/datetime expression
  581. *Output*: integer
  582. *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
  583. of a year is the first week with a majority (4 or more) of its days in January.
  584. [source, sql]
  585. --------------------------------------------------
  586. include-tagged::{sql-specs}/docs/docs.csv-spec[isoWeekOfYear]
  587. --------------------------------------------------
  588. [[sql-functions-datetime-minuteofday]]
  589. ==== `MINUTE_OF_DAY`
  590. .Synopsis:
  591. [source, sql]
  592. --------------------------------------------------
  593. MINUTE_OF_DAY(datetime_exp) <1>
  594. --------------------------------------------------
  595. *Input*:
  596. <1> date/datetime expression
  597. *Output*: integer
  598. *Description*: Extract the minute of the day from a date/datetime.
  599. [source, sql]
  600. --------------------------------------------------
  601. include-tagged::{sql-specs}/docs/docs.csv-spec[minuteOfDay]
  602. --------------------------------------------------
  603. [[sql-functions-datetime-minute]]
  604. ==== `MINUTE_OF_HOUR/MINUTE`
  605. .Synopsis:
  606. [source, sql]
  607. --------------------------------------------------
  608. MINUTE_OF_HOUR(datetime_exp) <1>
  609. --------------------------------------------------
  610. *Input*:
  611. <1> date/datetime expression
  612. *Output*: integer
  613. *Description*: Extract the minute of the hour from a date/datetime.
  614. [source, sql]
  615. --------------------------------------------------
  616. include-tagged::{sql-specs}/docs/docs.csv-spec[minuteOfHour]
  617. --------------------------------------------------
  618. [[sql-functions-datetime-month]]
  619. ==== `MONTH_OF_YEAR/MONTH`
  620. .Synopsis:
  621. [source, sql]
  622. --------------------------------------------------
  623. MONTH(datetime_exp) <1>
  624. --------------------------------------------------
  625. *Input*:
  626. <1> date/datetime expression
  627. *Output*: integer
  628. *Description*: Extract the month of the year from a date/datetime.
  629. [source, sql]
  630. --------------------------------------------------
  631. include-tagged::{sql-specs}/docs/docs.csv-spec[monthOfYear]
  632. --------------------------------------------------
  633. [[sql-functions-datetime-monthname]]
  634. ==== `MONTH_NAME/MONTHNAME`
  635. .Synopsis:
  636. [source, sql]
  637. --------------------------------------------------
  638. MONTH_NAME(datetime_exp) <1>
  639. --------------------------------------------------
  640. *Input*:
  641. <1> date/datetime expression
  642. *Output*: string
  643. *Description*: Extract the month from a date/datetime in text format (`January`, `February`...).
  644. [source, sql]
  645. --------------------------------------------------
  646. include-tagged::{sql-specs}/docs/docs.csv-spec[monthName]
  647. --------------------------------------------------
  648. [[sql-functions-now]]
  649. ==== `NOW`
  650. .Synopsis:
  651. [source, sql]
  652. --------------------------------------------------
  653. NOW()
  654. --------------------------------------------------
  655. *Input*: _none_
  656. *Output*: datetime
  657. *Description*: This function offers the same functionality as <<sql-functions-current-timestamp,CURRENT_TIMESTAMP()>> function: returns
  658. the datetime when the current query reached the server. This method always returns the same value for its every
  659. occurrence within the same query.
  660. [source, sql]
  661. --------------------------------------------------
  662. include-tagged::{sql-specs}/docs/docs.csv-spec[nowFunction]
  663. --------------------------------------------------
  664. Typically, this function (as well as its twin <<sql-functions-current-timestamp,CURRENT_TIMESTAMP())>> function is used
  665. for relative date/time filtering:
  666. [source, sql]
  667. --------------------------------------------------
  668. include-tagged::{sql-specs}/docs/docs.csv-spec[filterNow]
  669. --------------------------------------------------
  670. [[sql-functions-datetime-second]]
  671. ==== `SECOND_OF_MINUTE/SECOND`
  672. .Synopsis:
  673. [source, sql]
  674. --------------------------------------------------
  675. SECOND_OF_MINUTE(datetime_exp) <1>
  676. --------------------------------------------------
  677. *Input*:
  678. <1> date/datetime expression
  679. *Output*: integer
  680. *Description*: Extract the second of the minute from a date/datetime.
  681. [source, sql]
  682. --------------------------------------------------
  683. include-tagged::{sql-specs}/docs/docs.csv-spec[secondOfMinute]
  684. --------------------------------------------------
  685. [[sql-functions-datetime-quarter]]
  686. ==== `QUARTER`
  687. .Synopsis:
  688. [source, sql]
  689. --------------------------------------------------
  690. QUARTER(datetime_exp) <1>
  691. --------------------------------------------------
  692. *Input*:
  693. <1> date/datetime expression
  694. *Output*: integer
  695. *Description*: Extract the year quarter the date/datetime falls in.
  696. [source, sql]
  697. --------------------------------------------------
  698. include-tagged::{sql-specs}/docs/docs.csv-spec[quarter]
  699. --------------------------------------------------
  700. [[sql-functions-today]]
  701. ==== `TODAY`
  702. .Synopsis:
  703. [source, sql]
  704. --------------------------------------------------
  705. TODAY()
  706. --------------------------------------------------
  707. *Input*: _none_
  708. *Output*: date
  709. *Description*: This function offers the same functionality as <<sql-functions-current-date,CURRENT_DATE()>> function: returns
  710. the date when the current query reached the server. This method always returns the same value for its every occurrence
  711. within the same query.
  712. [source, sql]
  713. --------------------------------------------------
  714. include-tagged::{sql-specs}/docs/docs.csv-spec[todayFunction]
  715. --------------------------------------------------
  716. Typically, this function (as well as its twin <<sql-functions-current-timestamp,CURRENT_TIMESTAMP())>> function is used
  717. for relative date filtering:
  718. [source, sql]
  719. --------------------------------------------------
  720. include-tagged::{sql-specs}/docs/docs.csv-spec[filterToday]
  721. --------------------------------------------------
  722. [[sql-functions-datetime-week]]
  723. ==== `WEEK_OF_YEAR/WEEK`
  724. .Synopsis:
  725. [source, sql]
  726. --------------------------------------------------
  727. WEEK_OF_YEAR(datetime_exp) <1>
  728. --------------------------------------------------
  729. *Input*:
  730. <1> date/datetime expression
  731. *Output*: integer
  732. *Description*: Extract the week of the year from a date/datetime.
  733. [source, sql]
  734. --------------------------------------------------
  735. include-tagged::{sql-specs}/docs/docs.csv-spec[weekOfYear]
  736. --------------------------------------------------
  737. [[sql-functions-datetime-year]]
  738. ==== `YEAR`
  739. .Synopsis:
  740. [source, sql]
  741. --------------------------------------------------
  742. YEAR(datetime_exp) <1>
  743. --------------------------------------------------
  744. *Input*:
  745. <1> date/datetime expression
  746. *Output*: integer
  747. *Description*: Extract the year from a date/datetime.
  748. [source, sql]
  749. --------------------------------------------------
  750. include-tagged::{sql-specs}/docs/docs.csv-spec[year]
  751. --------------------------------------------------
  752. [[sql-functions-datetime-extract]]
  753. ==== `EXTRACT`
  754. .Synopsis:
  755. [source, sql]
  756. --------------------------------------------------
  757. EXTRACT(
  758. datetime_function <1>
  759. FROM datetime_exp) <2>
  760. --------------------------------------------------
  761. *Input*:
  762. <1> date/time function name
  763. <2> date/datetime expression
  764. *Output*: integer
  765. *Description*: Extract fields from a date/datetime by specifying the name of a <<sql-functions-datetime,datetime function>>.
  766. The following
  767. [source, sql]
  768. --------------------------------------------------
  769. include-tagged::{sql-specs}/docs/docs.csv-spec[extractDayOfYear]
  770. --------------------------------------------------
  771. is the equivalent to
  772. [source, sql]
  773. --------------------------------------------------
  774. include-tagged::{sql-specs}/docs/docs.csv-spec[dayOfYear]
  775. --------------------------------------------------