date-time.asciidoc 29 KB

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