date-time.asciidoc 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951
  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 (`+`, `-`, etc) 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/TIMESTAMPADD/TIMESTAMP_ADD`
  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-part]]
  249. ==== `DATE_PART/DATEPART`
  250. .Synopsis:
  251. [source, sql]
  252. --------------------------------------------------
  253. DATE_PART(
  254. string_exp, <1>
  255. datetime_exp) <2>
  256. --------------------------------------------------
  257. *Input*:
  258. <1> string expression denoting the unit to extract from the date/datetime
  259. <2> date/datetime expression
  260. *Output*: integer
  261. .Description:
  262. Extract the specified unit from a date/datetime. If any of the two arguments is `null` a `null` is returned.
  263. It's similar to <<sql-functions-datetime-extract>> but with different names and aliases for the units and
  264. provides more options (e.g.: `TZOFFSET`).
  265. [cols="^,^"]
  266. |===
  267. 2+h|Datetime units to extract
  268. s|unit
  269. s|abbreviations
  270. | year | years, yy, yyyy
  271. | quarter | quarters, qq, q
  272. | month | months, mm, m
  273. | dayofyear | dy, y
  274. | day | days, dd, d
  275. | week | weeks, wk, ww
  276. | weekday | weekdays, dw
  277. | hour | hours, hh
  278. | minute | minutes, mi, n
  279. | second | seconds, ss, s
  280. | millisecond | milliseconds, ms
  281. | microsecond | microseconds, mcs
  282. | nanosecond | nanoseconds, ns
  283. | tzoffset | tz
  284. |===
  285. [source, sql]
  286. --------------------------------------------------
  287. include-tagged::{sql-specs}/docs/docs.csv-spec[datePartDateTimeYears]
  288. --------------------------------------------------
  289. [source, sql]
  290. --------------------------------------------------
  291. include-tagged::{sql-specs}/docs/docs.csv-spec[datePartDateTimeMinutes]
  292. --------------------------------------------------
  293. [source, sql]
  294. --------------------------------------------------
  295. include-tagged::{sql-specs}/docs/docs.csv-spec[datePartDateQuarter]
  296. --------------------------------------------------
  297. [source, sql]
  298. --------------------------------------------------
  299. include-tagged::{sql-specs}/docs/docs.csv-spec[datePartDateMonth]
  300. --------------------------------------------------
  301. [NOTE]
  302. For `week` and `weekday` the unit is extracted using the non-ISO calculation, which means
  303. that a given week is considered to start from Sunday, not Monday.
  304. [source, sql]
  305. --------------------------------------------------
  306. include-tagged::{sql-specs}/docs/docs.csv-spec[datePartDateTimeWeek]
  307. --------------------------------------------------
  308. [NOTE]
  309. The `tzoffset` returns the total number of minutes (signed) that represent the time zone's offset.
  310. [source, sql]
  311. --------------------------------------------------
  312. include-tagged::{sql-specs}/docs/docs.csv-spec[datePartDateTimeTzOffsetPlus]
  313. --------------------------------------------------
  314. [source, sql]
  315. --------------------------------------------------
  316. include-tagged::{sql-specs}/docs/docs.csv-spec[datePartDateTimeTzOffsetMinus]
  317. --------------------------------------------------
  318. [[sql-functions-datetime-trunc]]
  319. ==== `DATE_TRUNC/DATETRUNC`
  320. .Synopsis:
  321. [source, sql]
  322. --------------------------------------------------
  323. DATE_TRUNC(
  324. string_exp, <1>
  325. datetime_exp) <2>
  326. --------------------------------------------------
  327. *Input*:
  328. <1> string expression denoting the unit to which the date/datetime should be truncated to
  329. <2> date/datetime expression
  330. *Output*: datetime
  331. .Description:
  332. Truncate the date/datetime to the specified unit by setting all fields that are less significant than the specified
  333. one to zero (or one, for day, day of week and month). If any of the two arguments is `null` a `null` is returned.
  334. [cols="^,^"]
  335. |===
  336. 2+h|Datetime truncation units
  337. s|unit
  338. s|abbreviations
  339. | millennium | millennia
  340. | century | centuries
  341. | decade | decades
  342. | year | years, yy, yyyy
  343. | quarter | quarters, qq, q
  344. | month | months, mm, m
  345. | week | weeks, wk, ww
  346. | day | days, dd, d
  347. | hour | hours, hh
  348. | minute | minutes, mi, n
  349. | second | seconds, ss, s
  350. | millisecond | milliseconds, ms
  351. | microsecond | microseconds, mcs
  352. | nanosecond | nanoseconds, ns
  353. |===
  354. [source, sql]
  355. --------------------------------------------------
  356. include-tagged::{sql-specs}/docs/docs.csv-spec[truncateDateTimeMillennium]
  357. --------------------------------------------------
  358. [source, sql]
  359. --------------------------------------------------
  360. include-tagged::{sql-specs}/docs/docs.csv-spec[truncateDateTimeWeek]
  361. --------------------------------------------------
  362. [source, sql]
  363. --------------------------------------------------
  364. include-tagged::{sql-specs}/docs/docs.csv-spec[truncateDateTimeMinutes]
  365. --------------------------------------------------
  366. [source, sql]
  367. --------------------------------------------------
  368. include-tagged::{sql-specs}/docs/docs.csv-spec[truncateDateDecades]
  369. --------------------------------------------------
  370. [source, sql]
  371. --------------------------------------------------
  372. include-tagged::{sql-specs}/docs/docs.csv-spec[truncateDateQuarter]
  373. --------------------------------------------------
  374. [[sql-functions-datetime-day]]
  375. ==== `DAY_OF_MONTH/DOM/DAY`
  376. .Synopsis:
  377. [source, sql]
  378. --------------------------------------------------
  379. DAY_OF_MONTH(datetime_exp) <1>
  380. --------------------------------------------------
  381. *Input*:
  382. <1> date/datetime expression
  383. *Output*: integer
  384. .Description:
  385. Extract the day of the month from a date/datetime.
  386. [source, sql]
  387. --------------------------------------------------
  388. include-tagged::{sql-specs}/docs/docs.csv-spec[dayOfMonth]
  389. --------------------------------------------------
  390. [[sql-functions-datetime-dow]]
  391. ==== `DAY_OF_WEEK/DAYOFWEEK/DOW`
  392. .Synopsis:
  393. [source, sql]
  394. --------------------------------------------------
  395. DAY_OF_WEEK(datetime_exp) <1>
  396. --------------------------------------------------
  397. *Input*:
  398. <1> date/datetime expression
  399. *Output*: integer
  400. .Description:
  401. Extract the day of the week from a date/datetime. Sunday is `1`, Monday is `2`, etc.
  402. [source, sql]
  403. --------------------------------------------------
  404. include-tagged::{sql-specs}/docs/docs.csv-spec[dayOfWeek]
  405. --------------------------------------------------
  406. [[sql-functions-datetime-doy]]
  407. ==== `DAY_OF_YEAR/DOY`
  408. .Synopsis:
  409. [source, sql]
  410. --------------------------------------------------
  411. DAY_OF_YEAR(datetime_exp) <1>
  412. --------------------------------------------------
  413. *Input*:
  414. <1> date/datetime expression
  415. *Output*: integer
  416. .Description:
  417. Extract the day of the year from a date/datetime.
  418. [source, sql]
  419. --------------------------------------------------
  420. include-tagged::{sql-specs}/docs/docs.csv-spec[dayOfYear]
  421. --------------------------------------------------
  422. [[sql-functions-datetime-dayname]]
  423. ==== `DAY_NAME/DAYNAME`
  424. .Synopsis:
  425. [source, sql]
  426. --------------------------------------------------
  427. DAY_NAME(datetime_exp) <1>
  428. --------------------------------------------------
  429. *Input*:
  430. <1> date/datetime expression
  431. *Output*: string
  432. .Description:
  433. Extract the day of the week from a date/datetime in text format (`Monday`, `Tuesday`...).
  434. [source, sql]
  435. --------------------------------------------------
  436. include-tagged::{sql-specs}/docs/docs.csv-spec[dayName]
  437. --------------------------------------------------
  438. [[sql-functions-datetime-hour]]
  439. ==== `HOUR_OF_DAY/HOUR`
  440. .Synopsis:
  441. [source, sql]
  442. --------------------------------------------------
  443. HOUR_OF_DAY(datetime_exp) <1>
  444. --------------------------------------------------
  445. *Input*:
  446. <1> date/datetime expression
  447. *Output*: integer
  448. .Description:
  449. Extract the hour of the day from a date/datetime.
  450. [source, sql]
  451. --------------------------------------------------
  452. include-tagged::{sql-specs}/docs/docs.csv-spec[hourOfDay]
  453. --------------------------------------------------
  454. [[sql-functions-datetime-isodow]]
  455. ==== `ISO_DAY_OF_WEEK/ISODAYOFWEEK/ISODOW/IDOW`
  456. .Synopsis:
  457. [source, sql]
  458. --------------------------------------------------
  459. ISO_DAY_OF_WEEK(datetime_exp) <1>
  460. --------------------------------------------------
  461. *Input*:
  462. <1> date/datetime expression
  463. *Output*: integer
  464. .Description:
  465. Extract the day of the week from a date/datetime, following the https://en.wikipedia.org/wiki/ISO_week_date[ISO 8601 standard].
  466. Monday is `1`, Tuesday is `2`, etc.
  467. [source, sql]
  468. --------------------------------------------------
  469. include-tagged::{sql-specs}/docs/docs.csv-spec[isoDayOfWeek]
  470. --------------------------------------------------
  471. [[sql-functions-datetime-isoweek]]
  472. ==== `ISO_WEEK_OF_YEAR/ISOWEEKOFYEAR/ISOWEEK/IWOY/IW`
  473. .Synopsis:
  474. [source, sql]
  475. --------------------------------------------------
  476. ISO_WEEK_OF_YEAR(datetime_exp) <1>
  477. --------------------------------------------------
  478. *Input*:
  479. <1> date/datetime expression
  480. *Output*: integer
  481. .Description:
  482. 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
  483. of a year is the first week with a majority (4 or more) of its days in January.
  484. [source, sql]
  485. --------------------------------------------------
  486. include-tagged::{sql-specs}/docs/docs.csv-spec[isoWeekOfYear]
  487. --------------------------------------------------
  488. [[sql-functions-datetime-minuteofday]]
  489. ==== `MINUTE_OF_DAY`
  490. .Synopsis:
  491. [source, sql]
  492. --------------------------------------------------
  493. MINUTE_OF_DAY(datetime_exp) <1>
  494. --------------------------------------------------
  495. *Input*:
  496. <1> date/datetime expression
  497. *Output*: integer
  498. .Description:
  499. Extract the minute of the day from a date/datetime.
  500. [source, sql]
  501. --------------------------------------------------
  502. include-tagged::{sql-specs}/docs/docs.csv-spec[minuteOfDay]
  503. --------------------------------------------------
  504. [[sql-functions-datetime-minute]]
  505. ==== `MINUTE_OF_HOUR/MINUTE`
  506. .Synopsis:
  507. [source, sql]
  508. --------------------------------------------------
  509. MINUTE_OF_HOUR(datetime_exp) <1>
  510. --------------------------------------------------
  511. *Input*:
  512. <1> date/datetime expression
  513. *Output*: integer
  514. .Description:
  515. Extract the minute of the hour from a date/datetime.
  516. [source, sql]
  517. --------------------------------------------------
  518. include-tagged::{sql-specs}/docs/docs.csv-spec[minuteOfHour]
  519. --------------------------------------------------
  520. [[sql-functions-datetime-month]]
  521. ==== `MONTH_OF_YEAR/MONTH`
  522. .Synopsis:
  523. [source, sql]
  524. --------------------------------------------------
  525. MONTH(datetime_exp) <1>
  526. --------------------------------------------------
  527. *Input*:
  528. <1> date/datetime expression
  529. *Output*: integer
  530. .Description:
  531. Extract the month of the year from a date/datetime.
  532. [source, sql]
  533. --------------------------------------------------
  534. include-tagged::{sql-specs}/docs/docs.csv-spec[monthOfYear]
  535. --------------------------------------------------
  536. [[sql-functions-datetime-monthname]]
  537. ==== `MONTH_NAME/MONTHNAME`
  538. .Synopsis:
  539. [source, sql]
  540. --------------------------------------------------
  541. MONTH_NAME(datetime_exp) <1>
  542. --------------------------------------------------
  543. *Input*:
  544. <1> date/datetime expression
  545. *Output*: string
  546. .Description:
  547. Extract the month from a date/datetime in text format (`January`, `February`...).
  548. [source, sql]
  549. --------------------------------------------------
  550. include-tagged::{sql-specs}/docs/docs.csv-spec[monthName]
  551. --------------------------------------------------
  552. [[sql-functions-now]]
  553. ==== `NOW`
  554. .Synopsis:
  555. [source, sql]
  556. --------------------------------------------------
  557. NOW()
  558. --------------------------------------------------
  559. *Input*: _none_
  560. *Output*: datetime
  561. .Description:
  562. This function offers the same functionality as <<sql-functions-current-timestamp,CURRENT_TIMESTAMP()>> function: returns
  563. the datetime when the current query reached the server. This method always returns the same value for its every
  564. occurrence within the same query.
  565. [source, sql]
  566. --------------------------------------------------
  567. include-tagged::{sql-specs}/docs/docs.csv-spec[nowFunction]
  568. --------------------------------------------------
  569. Typically, this function (as well as its twin <<sql-functions-current-timestamp,CURRENT_TIMESTAMP())>> function is used
  570. for relative date/time filtering:
  571. [source, sql]
  572. --------------------------------------------------
  573. include-tagged::{sql-specs}/docs/docs.csv-spec[filterNow]
  574. --------------------------------------------------
  575. [[sql-functions-datetime-second]]
  576. ==== `SECOND_OF_MINUTE/SECOND`
  577. .Synopsis:
  578. [source, sql]
  579. --------------------------------------------------
  580. SECOND_OF_MINUTE(datetime_exp) <1>
  581. --------------------------------------------------
  582. *Input*:
  583. <1> date/datetime expression
  584. *Output*: integer
  585. .Description:
  586. Extract the second of the minute from a date/datetime.
  587. [source, sql]
  588. --------------------------------------------------
  589. include-tagged::{sql-specs}/docs/docs.csv-spec[secondOfMinute]
  590. --------------------------------------------------
  591. [[sql-functions-datetime-quarter]]
  592. ==== `QUARTER`
  593. .Synopsis:
  594. [source, sql]
  595. --------------------------------------------------
  596. QUARTER(datetime_exp) <1>
  597. --------------------------------------------------
  598. *Input*:
  599. <1> date/datetime expression
  600. *Output*: integer
  601. .Description:
  602. Extract the year quarter the date/datetime falls in.
  603. [source, sql]
  604. --------------------------------------------------
  605. include-tagged::{sql-specs}/docs/docs.csv-spec[quarter]
  606. --------------------------------------------------
  607. [[sql-functions-today]]
  608. ==== `TODAY`
  609. .Synopsis:
  610. [source, sql]
  611. --------------------------------------------------
  612. TODAY()
  613. --------------------------------------------------
  614. *Input*: _none_
  615. *Output*: date
  616. .Description:
  617. This function offers the same functionality as <<sql-functions-current-date,CURRENT_DATE()>> function: returns
  618. the date when the current query reached the server. This method always returns the same value for its every occurrence
  619. within the same query.
  620. [source, sql]
  621. --------------------------------------------------
  622. include-tagged::{sql-specs}/docs/docs.csv-spec[todayFunction]
  623. --------------------------------------------------
  624. Typically, this function (as well as its twin <<sql-functions-current-timestamp,CURRENT_TIMESTAMP())>> function is used
  625. for relative date filtering:
  626. [source, sql]
  627. --------------------------------------------------
  628. include-tagged::{sql-specs}/docs/docs.csv-spec[filterToday]
  629. --------------------------------------------------
  630. [[sql-functions-datetime-week]]
  631. ==== `WEEK_OF_YEAR/WEEK`
  632. .Synopsis:
  633. [source, sql]
  634. --------------------------------------------------
  635. WEEK_OF_YEAR(datetime_exp) <1>
  636. --------------------------------------------------
  637. *Input*:
  638. <1> date/datetime expression
  639. *Output*: integer
  640. .Description:
  641. Extract the week of the year from a date/datetime.
  642. [source, sql]
  643. --------------------------------------------------
  644. include-tagged::{sql-specs}/docs/docs.csv-spec[weekOfYear]
  645. --------------------------------------------------
  646. [[sql-functions-datetime-year]]
  647. ==== `YEAR`
  648. .Synopsis:
  649. [source, sql]
  650. --------------------------------------------------
  651. YEAR(datetime_exp) <1>
  652. --------------------------------------------------
  653. *Input*:
  654. <1> date/datetime expression
  655. *Output*: integer
  656. .Description:
  657. Extract the year from a date/datetime.
  658. [source, sql]
  659. --------------------------------------------------
  660. include-tagged::{sql-specs}/docs/docs.csv-spec[year]
  661. --------------------------------------------------
  662. [[sql-functions-datetime-extract]]
  663. ==== `EXTRACT`
  664. .Synopsis:
  665. [source, sql]
  666. --------------------------------------------------
  667. EXTRACT(
  668. datetime_function <1>
  669. FROM datetime_exp) <2>
  670. --------------------------------------------------
  671. *Input*:
  672. <1> date/time function name
  673. <2> date/datetime expression
  674. *Output*: integer
  675. .Description:
  676. Extract fields from a date/datetime by specifying the name of a <<sql-functions-datetime,datetime function>>.
  677. The following
  678. [source, sql]
  679. --------------------------------------------------
  680. include-tagged::{sql-specs}/docs/docs.csv-spec[extractDayOfYear]
  681. --------------------------------------------------
  682. is the equivalent to
  683. [source, sql]
  684. --------------------------------------------------
  685. include-tagged::{sql-specs}/docs/docs.csv-spec[dayOfYear]
  686. --------------------------------------------------