date-time.asciidoc 29 KB

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