date-time.asciidoc 35 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147
  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-datetimeparse]]
  345. ==== `DATETIME_PARSE`
  346. .Synopsis:
  347. [source, sql]
  348. --------------------------------------------------
  349. DATETIME_PARSE(
  350. string_exp, <1>
  351. string_exp) <2>
  352. --------------------------------------------------
  353. *Input*:
  354. <1> datetime expression as a string
  355. <2> parsing pattern
  356. *Output*: datetime
  357. *Description*: Returns a datetime by parsing the 1st argument using the format specified in the 2nd argument. The parsing
  358. format pattern used is the one from
  359. https://docs.oracle.com/en/java/javase/14/docs/api/java.base/java/time/format/DateTimeFormatter.html[`java.time.format.DateTimeFormatter`].
  360. If any of the two arguments is `null` or an empty string `null` is returned.
  361. [NOTE]
  362. If the parsing pattern contains only date or only time units (e.g. 'dd/MM/uuuu', 'HH:mm:ss', etc.) an error is returned
  363. as the function needs to return a value of `datetime` type which must contain both.
  364. [source, sql]
  365. --------------------------------------------------
  366. include-tagged::{sql-specs}/docs/docs.csv-spec[dateTimeParse1]
  367. --------------------------------------------------
  368. [source, sql]
  369. --------------------------------------------------
  370. include-tagged::{sql-specs}/docs/docs.csv-spec[dateTimeParse2]
  371. --------------------------------------------------
  372. [NOTE]
  373. ====
  374. If timezone is not specified in the datetime string expression and the parsing pattern, the resulting `datetime` will have the
  375. time zone specified by the user through the <<sql-rest-fields-timezone,`time_zone`>>/<<jdbc-cfg-timezone,`timezone`>> REST/driver parameters
  376. with no conversion applied.
  377. [source, sql]
  378. --------------------------------------------------
  379. include-tagged::{sql-specs}/docs/docs.csv-spec[dateTimeParse3]
  380. --------------------------------------------------
  381. ====
  382. [[sql-functions-datetime-timeparse]]
  383. ==== `TIME_PARSE`
  384. .Synopsis:
  385. [source, sql]
  386. --------------------------------------------------
  387. TIME_PARSE(
  388. string_exp, <1>
  389. string_exp) <2>
  390. --------------------------------------------------
  391. *Input*:
  392. <1> time expression as a string
  393. <2> parsing pattern
  394. *Output*: time
  395. *Description*: Returns a time by parsing the 1st argument using the format specified in the 2nd argument. The parsing
  396. format pattern used is the one from
  397. https://docs.oracle.com/en/java/javase/14/docs/api/java.base/java/time/format/DateTimeFormatter.html[`java.time.format.DateTimeFormatter`].
  398. If any of the two arguments is `null` or an empty string `null` is returned.
  399. [NOTE]
  400. If the parsing pattern contains only date units (e.g. 'dd/MM/uuuu') an error is returned
  401. as the function needs to return a value of `time` type which will contain only time.
  402. [source, sql]
  403. --------------------------------------------------
  404. include-tagged::{sql-specs}/docs/docs.csv-spec[timeParse1]
  405. --------------------------------------------------
  406. [source, sql]
  407. --------------------------------------------------
  408. include-tagged::{sql-specs}/docs/docs.csv-spec[timeParse2]
  409. --------------------------------------------------
  410. [NOTE]
  411. ====
  412. If timezone is not specified in the time string expression and the parsing pattern,
  413. the resulting `time` will have the offset of the time zone specified by the user through the
  414. <<sql-rest-fields-timezone,`time_zone`>>/<<jdbc-cfg-timezone,`timezone`>> REST/driver
  415. parameters at the Unix epoch date (`1970-01-01`) with no conversion applied.
  416. [source, sql]
  417. --------------------------------------------------
  418. include-tagged::{sql-specs}/docs/docs.csv-spec[timeParse3]
  419. --------------------------------------------------
  420. ====
  421. [[sql-functions-datetime-part]]
  422. ==== `DATE_PART/DATEPART`
  423. .Synopsis:
  424. [source, sql]
  425. --------------------------------------------------
  426. DATE_PART(
  427. string_exp, <1>
  428. datetime_exp) <2>
  429. --------------------------------------------------
  430. *Input*:
  431. <1> string expression denoting the unit to extract from the date/datetime
  432. <2> date/datetime expression
  433. *Output*: integer
  434. *Description*: Extract the specified unit from a date/datetime. If any of the two arguments is `null` a `null` is returned.
  435. It's similar to <<sql-functions-datetime-extract>> but with different names and aliases for the units and
  436. provides more options (e.g.: `TZOFFSET`).
  437. [cols="^,^"]
  438. |===
  439. 2+h|Datetime units to extract
  440. s|unit
  441. s|abbreviations
  442. | year | years, yy, yyyy
  443. | quarter | quarters, qq, q
  444. | month | months, mm, m
  445. | dayofyear | dy, y
  446. | day | days, dd, d
  447. | week | weeks, wk, ww
  448. | weekday | weekdays, dw
  449. | hour | hours, hh
  450. | minute | minutes, mi, n
  451. | second | seconds, ss, s
  452. | millisecond | milliseconds, ms
  453. | microsecond | microseconds, mcs
  454. | nanosecond | nanoseconds, ns
  455. | tzoffset | tz
  456. |===
  457. [source, sql]
  458. --------------------------------------------------
  459. include-tagged::{sql-specs}/docs/docs.csv-spec[datePartDateTimeYears]
  460. --------------------------------------------------
  461. [source, sql]
  462. --------------------------------------------------
  463. include-tagged::{sql-specs}/docs/docs.csv-spec[datePartDateTimeMinutes]
  464. --------------------------------------------------
  465. [source, sql]
  466. --------------------------------------------------
  467. include-tagged::{sql-specs}/docs/docs.csv-spec[datePartDateQuarter]
  468. --------------------------------------------------
  469. [source, sql]
  470. --------------------------------------------------
  471. include-tagged::{sql-specs}/docs/docs.csv-spec[datePartDateMonth]
  472. --------------------------------------------------
  473. [NOTE]
  474. For `week` and `weekday` the unit is extracted using the non-ISO calculation, which means
  475. that a given week is considered to start from Sunday, not Monday.
  476. [source, sql]
  477. --------------------------------------------------
  478. include-tagged::{sql-specs}/docs/docs.csv-spec[datePartDateTimeWeek]
  479. --------------------------------------------------
  480. [NOTE]
  481. The `tzoffset` returns the total number of minutes (signed) that represent the time zone's offset.
  482. [source, sql]
  483. --------------------------------------------------
  484. include-tagged::{sql-specs}/docs/docs.csv-spec[datePartDateTimeTzOffsetPlus]
  485. --------------------------------------------------
  486. [source, sql]
  487. --------------------------------------------------
  488. include-tagged::{sql-specs}/docs/docs.csv-spec[datePartDateTimeTzOffsetMinus]
  489. --------------------------------------------------
  490. [[sql-functions-datetime-trunc]]
  491. ==== `DATE_TRUNC/DATETRUNC`
  492. .Synopsis:
  493. [source, sql]
  494. --------------------------------------------------
  495. DATE_TRUNC(
  496. string_exp, <1>
  497. datetime_exp/interval_exp) <2>
  498. --------------------------------------------------
  499. *Input*:
  500. <1> string expression denoting the unit to which the date/datetime/interval should be truncated to
  501. <2> date/datetime/interval expression
  502. *Output*: datetime/interval
  503. *Description*: Truncate the date/datetime/interval to the specified unit by setting all fields that are less significant than the specified
  504. one to zero (or one, for day, day of week and month). If any of the two arguments is `null` a `null` is returned.
  505. 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.
  506. [cols="^,^"]
  507. |===
  508. 2+h|Datetime truncation units
  509. s|unit
  510. s|abbreviations
  511. | millennium | millennia
  512. | century | centuries
  513. | decade | decades
  514. | year | years, yy, yyyy
  515. | quarter | quarters, qq, q
  516. | month | months, mm, m
  517. | week | weeks, wk, ww
  518. | day | days, dd, d
  519. | hour | hours, hh
  520. | minute | minutes, mi, n
  521. | second | seconds, ss, s
  522. | millisecond | milliseconds, ms
  523. | microsecond | microseconds, mcs
  524. | nanosecond | nanoseconds, ns
  525. |===
  526. [source, sql]
  527. --------------------------------------------------
  528. include-tagged::{sql-specs}/docs/docs.csv-spec[truncateDateTimeMillennium]
  529. --------------------------------------------------
  530. [source, sql]
  531. --------------------------------------------------
  532. include-tagged::{sql-specs}/docs/docs.csv-spec[truncateDateTimeWeek]
  533. --------------------------------------------------
  534. [source, sql]
  535. --------------------------------------------------
  536. include-tagged::{sql-specs}/docs/docs.csv-spec[truncateDateTimeMinutes]
  537. --------------------------------------------------
  538. [source, sql]
  539. --------------------------------------------------
  540. include-tagged::{sql-specs}/docs/docs.csv-spec[truncateDateDecades]
  541. --------------------------------------------------
  542. [source, sql]
  543. --------------------------------------------------
  544. include-tagged::{sql-specs}/docs/docs.csv-spec[truncateDateQuarter]
  545. --------------------------------------------------
  546. [source, sql]
  547. --------------------------------------------------
  548. include-tagged::{sql-specs}/docs/docs.csv-spec[truncateIntervalCenturies]
  549. --------------------------------------------------
  550. [source, sql]
  551. --------------------------------------------------
  552. include-tagged::{sql-specs}/docs/docs.csv-spec[truncateIntervalHour]
  553. --------------------------------------------------
  554. [source, sql]
  555. --------------------------------------------------
  556. include-tagged::{sql-specs}/docs/docs.csv-spec[truncateIntervalDay]
  557. --------------------------------------------------
  558. [[sql-functions-datetime-day]]
  559. ==== `DAY_OF_MONTH/DOM/DAY`
  560. .Synopsis:
  561. [source, sql]
  562. --------------------------------------------------
  563. DAY_OF_MONTH(datetime_exp) <1>
  564. --------------------------------------------------
  565. *Input*:
  566. <1> date/datetime expression
  567. *Output*: integer
  568. *Description*: Extract the day of the month from a date/datetime.
  569. [source, sql]
  570. --------------------------------------------------
  571. include-tagged::{sql-specs}/docs/docs.csv-spec[dayOfMonth]
  572. --------------------------------------------------
  573. [[sql-functions-datetime-dow]]
  574. ==== `DAY_OF_WEEK/DAYOFWEEK/DOW`
  575. .Synopsis:
  576. [source, sql]
  577. --------------------------------------------------
  578. DAY_OF_WEEK(datetime_exp) <1>
  579. --------------------------------------------------
  580. *Input*:
  581. <1> date/datetime expression
  582. *Output*: integer
  583. *Description*: Extract the day of the week from a date/datetime. Sunday is `1`, Monday is `2`, etc.
  584. [source, sql]
  585. --------------------------------------------------
  586. include-tagged::{sql-specs}/docs/docs.csv-spec[dayOfWeek]
  587. --------------------------------------------------
  588. [[sql-functions-datetime-doy]]
  589. ==== `DAY_OF_YEAR/DOY`
  590. .Synopsis:
  591. [source, sql]
  592. --------------------------------------------------
  593. DAY_OF_YEAR(datetime_exp) <1>
  594. --------------------------------------------------
  595. *Input*:
  596. <1> date/datetime expression
  597. *Output*: integer
  598. *Description*: Extract the day of the year from a date/datetime.
  599. [source, sql]
  600. --------------------------------------------------
  601. include-tagged::{sql-specs}/docs/docs.csv-spec[dayOfYear]
  602. --------------------------------------------------
  603. [[sql-functions-datetime-dayname]]
  604. ==== `DAY_NAME/DAYNAME`
  605. .Synopsis:
  606. [source, sql]
  607. --------------------------------------------------
  608. DAY_NAME(datetime_exp) <1>
  609. --------------------------------------------------
  610. *Input*:
  611. <1> date/datetime expression
  612. *Output*: string
  613. *Description*: Extract the day of the week from a date/datetime in text format (`Monday`, `Tuesday`...).
  614. [source, sql]
  615. --------------------------------------------------
  616. include-tagged::{sql-specs}/docs/docs.csv-spec[dayName]
  617. --------------------------------------------------
  618. [[sql-functions-datetime-hour]]
  619. ==== `HOUR_OF_DAY/HOUR`
  620. .Synopsis:
  621. [source, sql]
  622. --------------------------------------------------
  623. HOUR_OF_DAY(datetime_exp) <1>
  624. --------------------------------------------------
  625. *Input*:
  626. <1> date/datetime expression
  627. *Output*: integer
  628. *Description*: Extract the hour of the day from a date/datetime.
  629. [source, sql]
  630. --------------------------------------------------
  631. include-tagged::{sql-specs}/docs/docs.csv-spec[hourOfDay]
  632. --------------------------------------------------
  633. [[sql-functions-datetime-isodow]]
  634. ==== `ISO_DAY_OF_WEEK/ISODAYOFWEEK/ISODOW/IDOW`
  635. .Synopsis:
  636. [source, sql]
  637. --------------------------------------------------
  638. ISO_DAY_OF_WEEK(datetime_exp) <1>
  639. --------------------------------------------------
  640. *Input*:
  641. <1> date/datetime expression
  642. *Output*: integer
  643. *Description*: Extract the day of the week from a date/datetime, following the https://en.wikipedia.org/wiki/ISO_week_date[ISO 8601 standard].
  644. Monday is `1`, Tuesday is `2`, etc.
  645. [source, sql]
  646. --------------------------------------------------
  647. include-tagged::{sql-specs}/docs/docs.csv-spec[isoDayOfWeek]
  648. --------------------------------------------------
  649. [[sql-functions-datetime-isoweek]]
  650. ==== `ISO_WEEK_OF_YEAR/ISOWEEKOFYEAR/ISOWEEK/IWOY/IW`
  651. .Synopsis:
  652. [source, sql]
  653. --------------------------------------------------
  654. ISO_WEEK_OF_YEAR(datetime_exp) <1>
  655. --------------------------------------------------
  656. *Input*:
  657. <1> date/datetime expression
  658. *Output*: integer
  659. *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
  660. of a year is the first week with a majority (4 or more) of its days in January.
  661. [source, sql]
  662. --------------------------------------------------
  663. include-tagged::{sql-specs}/docs/docs.csv-spec[isoWeekOfYear]
  664. --------------------------------------------------
  665. [[sql-functions-datetime-minuteofday]]
  666. ==== `MINUTE_OF_DAY`
  667. .Synopsis:
  668. [source, sql]
  669. --------------------------------------------------
  670. MINUTE_OF_DAY(datetime_exp) <1>
  671. --------------------------------------------------
  672. *Input*:
  673. <1> date/datetime expression
  674. *Output*: integer
  675. *Description*: Extract the minute of the day from a date/datetime.
  676. [source, sql]
  677. --------------------------------------------------
  678. include-tagged::{sql-specs}/docs/docs.csv-spec[minuteOfDay]
  679. --------------------------------------------------
  680. [[sql-functions-datetime-minute]]
  681. ==== `MINUTE_OF_HOUR/MINUTE`
  682. .Synopsis:
  683. [source, sql]
  684. --------------------------------------------------
  685. MINUTE_OF_HOUR(datetime_exp) <1>
  686. --------------------------------------------------
  687. *Input*:
  688. <1> date/datetime expression
  689. *Output*: integer
  690. *Description*: Extract the minute of the hour from a date/datetime.
  691. [source, sql]
  692. --------------------------------------------------
  693. include-tagged::{sql-specs}/docs/docs.csv-spec[minuteOfHour]
  694. --------------------------------------------------
  695. [[sql-functions-datetime-month]]
  696. ==== `MONTH_OF_YEAR/MONTH`
  697. .Synopsis:
  698. [source, sql]
  699. --------------------------------------------------
  700. MONTH(datetime_exp) <1>
  701. --------------------------------------------------
  702. *Input*:
  703. <1> date/datetime expression
  704. *Output*: integer
  705. *Description*: Extract the month of the year from a date/datetime.
  706. [source, sql]
  707. --------------------------------------------------
  708. include-tagged::{sql-specs}/docs/docs.csv-spec[monthOfYear]
  709. --------------------------------------------------
  710. [[sql-functions-datetime-monthname]]
  711. ==== `MONTH_NAME/MONTHNAME`
  712. .Synopsis:
  713. [source, sql]
  714. --------------------------------------------------
  715. MONTH_NAME(datetime_exp) <1>
  716. --------------------------------------------------
  717. *Input*:
  718. <1> date/datetime expression
  719. *Output*: string
  720. *Description*: Extract the month from a date/datetime in text format (`January`, `February`...).
  721. [source, sql]
  722. --------------------------------------------------
  723. include-tagged::{sql-specs}/docs/docs.csv-spec[monthName]
  724. --------------------------------------------------
  725. [[sql-functions-now]]
  726. ==== `NOW`
  727. .Synopsis:
  728. [source, sql]
  729. --------------------------------------------------
  730. NOW()
  731. --------------------------------------------------
  732. *Input*: _none_
  733. *Output*: datetime
  734. *Description*: This function offers the same functionality as <<sql-functions-current-timestamp,CURRENT_TIMESTAMP()>> function: returns
  735. the datetime when the current query reached the server. This method always returns the same value for its every
  736. occurrence within the same query.
  737. [source, sql]
  738. --------------------------------------------------
  739. include-tagged::{sql-specs}/docs/docs.csv-spec[nowFunction]
  740. --------------------------------------------------
  741. Typically, this function (as well as its twin <<sql-functions-current-timestamp,CURRENT_TIMESTAMP())>> function is used
  742. for relative date/time filtering:
  743. [source, sql]
  744. --------------------------------------------------
  745. include-tagged::{sql-specs}/docs/docs.csv-spec[filterNow]
  746. --------------------------------------------------
  747. [[sql-functions-datetime-second]]
  748. ==== `SECOND_OF_MINUTE/SECOND`
  749. .Synopsis:
  750. [source, sql]
  751. --------------------------------------------------
  752. SECOND_OF_MINUTE(datetime_exp) <1>
  753. --------------------------------------------------
  754. *Input*:
  755. <1> date/datetime expression
  756. *Output*: integer
  757. *Description*: Extract the second of the minute from a date/datetime.
  758. [source, sql]
  759. --------------------------------------------------
  760. include-tagged::{sql-specs}/docs/docs.csv-spec[secondOfMinute]
  761. --------------------------------------------------
  762. [[sql-functions-datetime-quarter]]
  763. ==== `QUARTER`
  764. .Synopsis:
  765. [source, sql]
  766. --------------------------------------------------
  767. QUARTER(datetime_exp) <1>
  768. --------------------------------------------------
  769. *Input*:
  770. <1> date/datetime expression
  771. *Output*: integer
  772. *Description*: Extract the year quarter the date/datetime falls in.
  773. [source, sql]
  774. --------------------------------------------------
  775. include-tagged::{sql-specs}/docs/docs.csv-spec[quarter]
  776. --------------------------------------------------
  777. [[sql-functions-today]]
  778. ==== `TODAY`
  779. .Synopsis:
  780. [source, sql]
  781. --------------------------------------------------
  782. TODAY()
  783. --------------------------------------------------
  784. *Input*: _none_
  785. *Output*: date
  786. *Description*: This function offers the same functionality as <<sql-functions-current-date,CURRENT_DATE()>> function: returns
  787. the date when the current query reached the server. This method always returns the same value for its every occurrence
  788. within the same query.
  789. [source, sql]
  790. --------------------------------------------------
  791. include-tagged::{sql-specs}/docs/docs.csv-spec[todayFunction]
  792. --------------------------------------------------
  793. Typically, this function (as well as its twin <<sql-functions-current-timestamp,CURRENT_TIMESTAMP())>> function is used
  794. for relative date filtering:
  795. [source, sql]
  796. --------------------------------------------------
  797. include-tagged::{sql-specs}/docs/docs.csv-spec[filterToday]
  798. --------------------------------------------------
  799. [[sql-functions-datetime-week]]
  800. ==== `WEEK_OF_YEAR/WEEK`
  801. .Synopsis:
  802. [source, sql]
  803. --------------------------------------------------
  804. WEEK_OF_YEAR(datetime_exp) <1>
  805. --------------------------------------------------
  806. *Input*:
  807. <1> date/datetime expression
  808. *Output*: integer
  809. *Description*: Extract the week of the year from a date/datetime.
  810. [source, sql]
  811. --------------------------------------------------
  812. include-tagged::{sql-specs}/docs/docs.csv-spec[weekOfYear]
  813. --------------------------------------------------
  814. [[sql-functions-datetime-year]]
  815. ==== `YEAR`
  816. .Synopsis:
  817. [source, sql]
  818. --------------------------------------------------
  819. YEAR(datetime_exp) <1>
  820. --------------------------------------------------
  821. *Input*:
  822. <1> date/datetime expression
  823. *Output*: integer
  824. *Description*: Extract the year from a date/datetime.
  825. [source, sql]
  826. --------------------------------------------------
  827. include-tagged::{sql-specs}/docs/docs.csv-spec[year]
  828. --------------------------------------------------
  829. [[sql-functions-datetime-extract]]
  830. ==== `EXTRACT`
  831. .Synopsis:
  832. [source, sql]
  833. --------------------------------------------------
  834. EXTRACT(
  835. datetime_function <1>
  836. FROM datetime_exp) <2>
  837. --------------------------------------------------
  838. *Input*:
  839. <1> date/time function name
  840. <2> date/datetime expression
  841. *Output*: integer
  842. *Description*: Extract fields from a date/datetime by specifying the name of a <<sql-functions-datetime,datetime function>>.
  843. The following
  844. [source, sql]
  845. --------------------------------------------------
  846. include-tagged::{sql-specs}/docs/docs.csv-spec[extractDayOfYear]
  847. --------------------------------------------------
  848. is the equivalent to
  849. [source, sql]
  850. --------------------------------------------------
  851. include-tagged::{sql-specs}/docs/docs.csv-spec[dayOfYear]
  852. --------------------------------------------------