date-time.asciidoc 32 KB

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