date-time.asciidoc 33 KB

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