date-time.asciidoc 40 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290
  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-dateparse]]
  313. ==== `DATE_PARSE`
  314. .Synopsis:
  315. [source, sql]
  316. --------------------------------------------------
  317. DATE_PARSE(
  318. string_exp, <1>
  319. string_exp) <2>
  320. --------------------------------------------------
  321. *Input*:
  322. <1> date expression as a string
  323. <2> parsing pattern
  324. *Output*: date
  325. *Description*: Returns a date by parsing the 1st argument using the format specified in the 2nd argument. The parsing
  326. format pattern used is the one from
  327. https://docs.oracle.com/en/java/javase/14/docs/api/java.base/java/time/format/DateTimeFormatter.html[`java.time.format.DateTimeFormatter`].
  328. If any of the two arguments is `null` or an empty string, then `null` is returned.
  329. [NOTE]
  330. If the parsing pattern does not contain all valid date units (e.g. 'HH:mm:ss', 'dd-MM HH:mm:ss', etc.) an error is returned
  331. as the function needs to return a value of `date` type which will contain date part.
  332. [source, sql]
  333. --------------------------------------------------
  334. include-tagged::{sql-specs}/docs/docs.csv-spec[dateParse1]
  335. --------------------------------------------------
  336. [NOTE]
  337. ====
  338. The resulting `date` will have the time zone specified by the user through the
  339. <<sql-rest-fields-timezone,`time_zone`>>/<<jdbc-cfg-timezone,`timezone`>> REST/driver parameters
  340. with no conversion applied.
  341. [source, sql]
  342. --------------------------------------------------
  343. include-tagged::{sql-specs}/docs/docs.csv-spec[dateParse2]
  344. --------------------------------------------------
  345. ====
  346. [[sql-functions-datetime-datetimeformat]]
  347. ==== `DATETIME_FORMAT`
  348. .Synopsis:
  349. [source, sql]
  350. --------------------------------------------------
  351. DATETIME_FORMAT(
  352. date_exp/datetime_exp/time_exp, <1>
  353. string_exp) <2>
  354. --------------------------------------------------
  355. *Input*:
  356. <1> date/datetime/time expression
  357. <2> format pattern
  358. *Output*: string
  359. *Description*: Returns the date/datetime/time as a string using the format specified in the 2nd argument. The formatting
  360. pattern used is the one from
  361. https://docs.oracle.com/en/java/javase/14/docs/api/java.base/java/time/format/DateTimeFormatter.html[`java.time.format.DateTimeFormatter`].
  362. If any of the two arguments is `null` or the pattern is an empty string `null` is returned.
  363. [NOTE]
  364. If the 1st argument is of type `time`, then pattern specified by the 2nd argument cannot contain date related units
  365. (e.g. 'dd', 'MM', 'YYYY', etc.). If it contains such units an error is returned.
  366. [source, sql]
  367. --------------------------------------------------
  368. include-tagged::{sql-specs}/docs/docs.csv-spec[dateTimeFormatDate]
  369. --------------------------------------------------
  370. [source, sql]
  371. --------------------------------------------------
  372. include-tagged::{sql-specs}/docs/docs.csv-spec[dateTimeFormatDateTime]
  373. --------------------------------------------------
  374. [source, sql]
  375. --------------------------------------------------
  376. include-tagged::{sql-specs}/docs/docs.csv-spec[dateTimeFormatTime]
  377. --------------------------------------------------
  378. [[sql-functions-datetime-datetimeparse]]
  379. ==== `DATETIME_PARSE`
  380. .Synopsis:
  381. [source, sql]
  382. --------------------------------------------------
  383. DATETIME_PARSE(
  384. string_exp, <1>
  385. string_exp) <2>
  386. --------------------------------------------------
  387. *Input*:
  388. <1> datetime expression as a string
  389. <2> parsing pattern
  390. *Output*: datetime
  391. *Description*: Returns a datetime by parsing the 1st argument using the format specified in the 2nd argument. The parsing
  392. format pattern used is the one from
  393. https://docs.oracle.com/en/java/javase/14/docs/api/java.base/java/time/format/DateTimeFormatter.html[`java.time.format.DateTimeFormatter`].
  394. If any of the two arguments is `null` or an empty string `null` is returned.
  395. [NOTE]
  396. If the parsing pattern contains only date or only time units (e.g. 'dd/MM/uuuu', 'HH:mm:ss', etc.) an error is returned
  397. as the function needs to return a value of `datetime` type which must contain both.
  398. [source, sql]
  399. --------------------------------------------------
  400. include-tagged::{sql-specs}/docs/docs.csv-spec[dateTimeParse1]
  401. --------------------------------------------------
  402. [source, sql]
  403. --------------------------------------------------
  404. include-tagged::{sql-specs}/docs/docs.csv-spec[dateTimeParse2]
  405. --------------------------------------------------
  406. [NOTE]
  407. ====
  408. If timezone is not specified in the datetime string expression and the parsing pattern, the resulting `datetime` will have the
  409. time zone specified by the user through the <<sql-rest-fields-timezone,`time_zone`>>/<<jdbc-cfg-timezone,`timezone`>> REST/driver parameters
  410. with no conversion applied.
  411. [source, sql]
  412. --------------------------------------------------
  413. include-tagged::{sql-specs}/docs/docs.csv-spec[dateTimeParse3]
  414. --------------------------------------------------
  415. ====
  416. [[sql-functions-datetime-timeparse]]
  417. ==== `TIME_PARSE`
  418. .Synopsis:
  419. [source, sql]
  420. --------------------------------------------------
  421. TIME_PARSE(
  422. string_exp, <1>
  423. string_exp) <2>
  424. --------------------------------------------------
  425. *Input*:
  426. <1> time expression as a string
  427. <2> parsing pattern
  428. *Output*: time
  429. *Description*: Returns a time by parsing the 1st argument using the format specified in the 2nd argument. The parsing
  430. format pattern used is the one from
  431. https://docs.oracle.com/en/java/javase/14/docs/api/java.base/java/time/format/DateTimeFormatter.html[`java.time.format.DateTimeFormatter`].
  432. If any of the two arguments is `null` or an empty string `null` is returned.
  433. [NOTE]
  434. If the parsing pattern contains only date units (e.g. 'dd/MM/uuuu') an error is returned
  435. as the function needs to return a value of `time` type which will contain only time.
  436. [source, sql]
  437. --------------------------------------------------
  438. include-tagged::{sql-specs}/docs/docs.csv-spec[timeParse1]
  439. --------------------------------------------------
  440. [source, sql]
  441. --------------------------------------------------
  442. include-tagged::{sql-specs}/docs/docs.csv-spec[timeParse2]
  443. --------------------------------------------------
  444. [NOTE]
  445. ====
  446. If timezone is not specified in the time string expression and the parsing pattern,
  447. the resulting `time` will have the offset of the time zone specified by the user through the
  448. <<sql-rest-fields-timezone,`time_zone`>>/<<jdbc-cfg-timezone,`timezone`>> REST/driver
  449. parameters at the Unix epoch date (`1970-01-01`) with no conversion applied.
  450. [source, sql]
  451. --------------------------------------------------
  452. include-tagged::{sql-specs}/docs/docs.csv-spec[timeParse3]
  453. --------------------------------------------------
  454. ====
  455. [[sql-functions-datetime-part]]
  456. ==== `DATE_PART/DATEPART`
  457. .Synopsis:
  458. [source, sql]
  459. --------------------------------------------------
  460. DATE_PART(
  461. string_exp, <1>
  462. datetime_exp) <2>
  463. --------------------------------------------------
  464. *Input*:
  465. <1> string expression denoting the unit to extract from the date/datetime
  466. <2> date/datetime expression
  467. *Output*: integer
  468. *Description*: Extract the specified unit from a date/datetime. If any of the two arguments is `null` a `null` is returned.
  469. It's similar to <<sql-functions-datetime-extract>> but with different names and aliases for the units and
  470. provides more options (e.g.: `TZOFFSET`).
  471. [cols="^,^"]
  472. |===
  473. 2+h|Datetime units to extract
  474. s|unit
  475. s|abbreviations
  476. | year | years, yy, yyyy
  477. | quarter | quarters, qq, q
  478. | month | months, mm, m
  479. | dayofyear | dy, y
  480. | day | days, dd, d
  481. | week | weeks, wk, ww
  482. | weekday | weekdays, dw
  483. | hour | hours, hh
  484. | minute | minutes, mi, n
  485. | second | seconds, ss, s
  486. | millisecond | milliseconds, ms
  487. | microsecond | microseconds, mcs
  488. | nanosecond | nanoseconds, ns
  489. | tzoffset | tz
  490. |===
  491. [source, sql]
  492. --------------------------------------------------
  493. include-tagged::{sql-specs}/docs/docs.csv-spec[datePartDateTimeYears]
  494. --------------------------------------------------
  495. [source, sql]
  496. --------------------------------------------------
  497. include-tagged::{sql-specs}/docs/docs.csv-spec[datePartDateTimeMinutes]
  498. --------------------------------------------------
  499. [source, sql]
  500. --------------------------------------------------
  501. include-tagged::{sql-specs}/docs/docs.csv-spec[datePartDateQuarter]
  502. --------------------------------------------------
  503. [source, sql]
  504. --------------------------------------------------
  505. include-tagged::{sql-specs}/docs/docs.csv-spec[datePartDateMonth]
  506. --------------------------------------------------
  507. [NOTE]
  508. For `week` and `weekday` the unit is extracted using the non-ISO calculation, which means
  509. that a given week is considered to start from Sunday, not Monday.
  510. [source, sql]
  511. --------------------------------------------------
  512. include-tagged::{sql-specs}/docs/docs.csv-spec[datePartDateTimeWeek]
  513. --------------------------------------------------
  514. [NOTE]
  515. The `tzoffset` returns the total number of minutes (signed) that represent the time zone's offset.
  516. [source, sql]
  517. --------------------------------------------------
  518. include-tagged::{sql-specs}/docs/docs.csv-spec[datePartDateTimeTzOffsetPlus]
  519. --------------------------------------------------
  520. [source, sql]
  521. --------------------------------------------------
  522. include-tagged::{sql-specs}/docs/docs.csv-spec[datePartDateTimeTzOffsetMinus]
  523. --------------------------------------------------
  524. [[sql-functions-datetime-trunc]]
  525. ==== `DATE_TRUNC/DATETRUNC`
  526. .Synopsis:
  527. [source, sql]
  528. --------------------------------------------------
  529. DATE_TRUNC(
  530. string_exp, <1>
  531. datetime_exp/interval_exp) <2>
  532. --------------------------------------------------
  533. *Input*:
  534. <1> string expression denoting the unit to which the date/datetime/interval should be truncated to
  535. <2> date/datetime/interval expression
  536. *Output*: datetime/interval
  537. *Description*: Truncate the date/datetime/interval to the specified unit by setting all fields that are less significant than the specified
  538. one to zero (or one, for day, day of week and month). If any of the two arguments is `null` a `null` is returned.
  539. If the first argument is `week` and the second argument is of `interval` type, an error is thrown since the `interval` data type doesn't support a `week` time unit.
  540. [cols="^,^"]
  541. |===
  542. 2+h|Datetime truncation units
  543. s|unit
  544. s|abbreviations
  545. | millennium | millennia
  546. | century | centuries
  547. | decade | decades
  548. | year | years, yy, yyyy
  549. | quarter | quarters, qq, q
  550. | month | months, mm, m
  551. | week | weeks, wk, ww
  552. | day | days, dd, d
  553. | hour | hours, hh
  554. | minute | minutes, mi, n
  555. | second | seconds, ss, s
  556. | millisecond | milliseconds, ms
  557. | microsecond | microseconds, mcs
  558. | nanosecond | nanoseconds, ns
  559. |===
  560. [source, sql]
  561. --------------------------------------------------
  562. include-tagged::{sql-specs}/docs/docs.csv-spec[truncateDateTimeMillennium]
  563. --------------------------------------------------
  564. [source, sql]
  565. --------------------------------------------------
  566. include-tagged::{sql-specs}/docs/docs.csv-spec[truncateDateTimeWeek]
  567. --------------------------------------------------
  568. [source, sql]
  569. --------------------------------------------------
  570. include-tagged::{sql-specs}/docs/docs.csv-spec[truncateDateTimeMinutes]
  571. --------------------------------------------------
  572. [source, sql]
  573. --------------------------------------------------
  574. include-tagged::{sql-specs}/docs/docs.csv-spec[truncateDateDecades]
  575. --------------------------------------------------
  576. [source, sql]
  577. --------------------------------------------------
  578. include-tagged::{sql-specs}/docs/docs.csv-spec[truncateDateQuarter]
  579. --------------------------------------------------
  580. [source, sql]
  581. --------------------------------------------------
  582. include-tagged::{sql-specs}/docs/docs.csv-spec[truncateIntervalCenturies]
  583. --------------------------------------------------
  584. [source, sql]
  585. --------------------------------------------------
  586. include-tagged::{sql-specs}/docs/docs.csv-spec[truncateIntervalHour]
  587. --------------------------------------------------
  588. [source, sql]
  589. --------------------------------------------------
  590. include-tagged::{sql-specs}/docs/docs.csv-spec[truncateIntervalDay]
  591. --------------------------------------------------
  592. [[sql-functions-datetime-format]]
  593. ==== `FORMAT`
  594. .Synopsis:
  595. [source, sql]
  596. --------------------------------------------------
  597. FORMAT(
  598. date_exp/datetime_exp/time_exp, <1>
  599. string_exp) <2>
  600. --------------------------------------------------
  601. *Input*:
  602. <1> date/datetime/time expression
  603. <2> format pattern
  604. *Output*: string
  605. *Description*: Returns the date/datetime/time as a string using the
  606. https://docs.microsoft.com/en-us/sql/t-sql/functions/format-transact-sql#arguments[format] specified in the 2nd argument. The formatting
  607. pattern used is the one from
  608. https://docs.microsoft.com/en-us/dotnet/standard/base-types/custom-date-and-time-format-strings[Microsoft SQL Server Format Specification].
  609. If any of the two arguments is `null` or the pattern is an empty string `null` is returned.
  610. [NOTE]
  611. If the 1st argument is of type `time`, then pattern specified by the 2nd argument cannot contain date related units
  612. (e.g. 'dd', 'MM', 'YYYY', etc.). If it contains such units an error is returned. +
  613. Format specifier `F` will be working similar to format specifier `f`.
  614. It will return the fractional part of seconds, and the number of digits will be same as of the number of `Fs` provided as input (up to 9 digits).
  615. Result will contain `0` appended in the end to match with number of `F` provided.
  616. e.g.: for a time part `10:20:30.1234` and pattern `HH:mm:ss.FFFFFF`, the output string of the function would be: `10:20:30.123400`. +
  617. Format specifier `y` will return year-of-era instead of one/two low-order digits.
  618. eg.: For year `2009`, `y` will be returning `2009` instead of `9`. For year `43`, `y` format specifier will return `43`.
  619. - Special characters like `"` , `\` and `%` will be returned as it is without any change. eg.: formatting date `17-sep-2020` with `%M` will return `%9`
  620. [source, sql]
  621. --------------------------------------------------
  622. include-tagged::{sql-specs}/docs/docs.csv-spec[formatDate]
  623. --------------------------------------------------
  624. [source, sql]
  625. --------------------------------------------------
  626. include-tagged::{sql-specs}/docs/docs.csv-spec[formatDateTime]
  627. --------------------------------------------------
  628. [source, sql]
  629. --------------------------------------------------
  630. include-tagged::{sql-specs}/docs/docs.csv-spec[formatTime]
  631. --------------------------------------------------
  632. [[sql-functions-datetime-to_char]]
  633. ==== `TO_CHAR`
  634. .Synopsis:
  635. [source, sql]
  636. --------------------------------------------------
  637. TO_CHAR(
  638. date_exp/datetime_exp/time_exp, <1>
  639. string_exp) <2>
  640. --------------------------------------------------
  641. *Input*:
  642. <1> date/datetime/time expression
  643. <2> format pattern
  644. *Output*: string
  645. *Description*: Returns the date/datetime/time as a string using the format specified in the 2nd argument. The formatting
  646. pattern conforms to
  647. https://www.postgresql.org/docs/13/functions-formatting.html[PostgreSQL Template Patterns for Date/Time Formatting].
  648. If any of the two arguments is `null` or the pattern is an empty string `null` is returned.
  649. [NOTE]
  650. If the 1st argument is of type `time`, then the pattern specified by the 2nd argument cannot contain date related units
  651. (e.g. 'dd', 'MM', 'YYYY', etc.). If it contains such units an error is returned. +
  652. The result of the patterns `TZ` and `tz` (time zone abbreviations) in some cases differ from the results returned by the `TO_CHAR`
  653. in PostgreSQL. The reason is that the time zone abbreviations specified by the JDK are different from the ones specified by PostgreSQL.
  654. This function might show an actual time zone abbreviation instead of the generic `LMT` or empty string or offset returned by the PostgreSQL
  655. implementation. The summer/daylight markers might also differ between the two implementations (e.g. will show `HT` instead of `HST`
  656. for Hawaii). +
  657. The `FX`, `TM`, `SP` pattern modifiers are not supported and will show up as `FX`, `TM`, `SP` literals in the output.
  658. [source, sql]
  659. --------------------------------------------------
  660. include-tagged::{sql-specs}/docs/docs.csv-spec[toCharDate]
  661. --------------------------------------------------
  662. [source, sql]
  663. --------------------------------------------------
  664. include-tagged::{sql-specs}/docs/docs.csv-spec[toCharDateTime]
  665. --------------------------------------------------
  666. [source, sql]
  667. --------------------------------------------------
  668. include-tagged::{sql-specs}/docs/docs.csv-spec[toCharTime]
  669. --------------------------------------------------
  670. [[sql-functions-datetime-day]]
  671. ==== `DAY_OF_MONTH/DOM/DAY`
  672. .Synopsis:
  673. [source, sql]
  674. --------------------------------------------------
  675. DAY_OF_MONTH(datetime_exp) <1>
  676. --------------------------------------------------
  677. *Input*:
  678. <1> date/datetime expression
  679. *Output*: integer
  680. *Description*: Extract the day of the month from a date/datetime.
  681. [source, sql]
  682. --------------------------------------------------
  683. include-tagged::{sql-specs}/docs/docs.csv-spec[dayOfMonth]
  684. --------------------------------------------------
  685. [[sql-functions-datetime-dow]]
  686. ==== `DAY_OF_WEEK/DAYOFWEEK/DOW`
  687. .Synopsis:
  688. [source, sql]
  689. --------------------------------------------------
  690. DAY_OF_WEEK(datetime_exp) <1>
  691. --------------------------------------------------
  692. *Input*:
  693. <1> date/datetime expression
  694. *Output*: integer
  695. *Description*: Extract the day of the week from a date/datetime. Sunday is `1`, Monday is `2`, etc.
  696. [source, sql]
  697. --------------------------------------------------
  698. include-tagged::{sql-specs}/docs/docs.csv-spec[dayOfWeek]
  699. --------------------------------------------------
  700. [[sql-functions-datetime-doy]]
  701. ==== `DAY_OF_YEAR/DOY`
  702. .Synopsis:
  703. [source, sql]
  704. --------------------------------------------------
  705. DAY_OF_YEAR(datetime_exp) <1>
  706. --------------------------------------------------
  707. *Input*:
  708. <1> date/datetime expression
  709. *Output*: integer
  710. *Description*: Extract the day of the year from a date/datetime.
  711. [source, sql]
  712. --------------------------------------------------
  713. include-tagged::{sql-specs}/docs/docs.csv-spec[dayOfYear]
  714. --------------------------------------------------
  715. [[sql-functions-datetime-dayname]]
  716. ==== `DAY_NAME/DAYNAME`
  717. .Synopsis:
  718. [source, sql]
  719. --------------------------------------------------
  720. DAY_NAME(datetime_exp) <1>
  721. --------------------------------------------------
  722. *Input*:
  723. <1> date/datetime expression
  724. *Output*: string
  725. *Description*: Extract the day of the week from a date/datetime in text format (`Monday`, `Tuesday`...).
  726. [source, sql]
  727. --------------------------------------------------
  728. include-tagged::{sql-specs}/docs/docs.csv-spec[dayName]
  729. --------------------------------------------------
  730. [[sql-functions-datetime-hour]]
  731. ==== `HOUR_OF_DAY/HOUR`
  732. .Synopsis:
  733. [source, sql]
  734. --------------------------------------------------
  735. HOUR_OF_DAY(datetime_exp) <1>
  736. --------------------------------------------------
  737. *Input*:
  738. <1> date/datetime expression
  739. *Output*: integer
  740. *Description*: Extract the hour of the day from a date/datetime.
  741. [source, sql]
  742. --------------------------------------------------
  743. include-tagged::{sql-specs}/docs/docs.csv-spec[hourOfDay]
  744. --------------------------------------------------
  745. [[sql-functions-datetime-isodow]]
  746. ==== `ISO_DAY_OF_WEEK/ISODAYOFWEEK/ISODOW/IDOW`
  747. .Synopsis:
  748. [source, sql]
  749. --------------------------------------------------
  750. ISO_DAY_OF_WEEK(datetime_exp) <1>
  751. --------------------------------------------------
  752. *Input*:
  753. <1> date/datetime expression
  754. *Output*: integer
  755. *Description*: Extract the day of the week from a date/datetime, following the {wikipedia}/ISO_week_date[ISO 8601 standard].
  756. Monday is `1`, Tuesday is `2`, etc.
  757. [source, sql]
  758. --------------------------------------------------
  759. include-tagged::{sql-specs}/docs/docs.csv-spec[isoDayOfWeek]
  760. --------------------------------------------------
  761. [[sql-functions-datetime-isoweek]]
  762. ==== `ISO_WEEK_OF_YEAR/ISOWEEKOFYEAR/ISOWEEK/IWOY/IW`
  763. .Synopsis:
  764. [source, sql]
  765. --------------------------------------------------
  766. ISO_WEEK_OF_YEAR(datetime_exp) <1>
  767. --------------------------------------------------
  768. *Input*:
  769. <1> date/datetime expression
  770. *Output*: integer
  771. *Description*: Extract the week of the year from a date/datetime, following {wikipedia}/ISO_week_date[ISO 8601 standard]. The first week
  772. of a year is the first week with a majority (4 or more) of its days in January.
  773. [source, sql]
  774. --------------------------------------------------
  775. include-tagged::{sql-specs}/docs/docs.csv-spec[isoWeekOfYear]
  776. --------------------------------------------------
  777. [[sql-functions-datetime-minuteofday]]
  778. ==== `MINUTE_OF_DAY`
  779. .Synopsis:
  780. [source, sql]
  781. --------------------------------------------------
  782. MINUTE_OF_DAY(datetime_exp) <1>
  783. --------------------------------------------------
  784. *Input*:
  785. <1> date/datetime expression
  786. *Output*: integer
  787. *Description*: Extract the minute of the day from a date/datetime.
  788. [source, sql]
  789. --------------------------------------------------
  790. include-tagged::{sql-specs}/docs/docs.csv-spec[minuteOfDay]
  791. --------------------------------------------------
  792. [[sql-functions-datetime-minute]]
  793. ==== `MINUTE_OF_HOUR/MINUTE`
  794. .Synopsis:
  795. [source, sql]
  796. --------------------------------------------------
  797. MINUTE_OF_HOUR(datetime_exp) <1>
  798. --------------------------------------------------
  799. *Input*:
  800. <1> date/datetime expression
  801. *Output*: integer
  802. *Description*: Extract the minute of the hour from a date/datetime.
  803. [source, sql]
  804. --------------------------------------------------
  805. include-tagged::{sql-specs}/docs/docs.csv-spec[minuteOfHour]
  806. --------------------------------------------------
  807. [[sql-functions-datetime-month]]
  808. ==== `MONTH_OF_YEAR/MONTH`
  809. .Synopsis:
  810. [source, sql]
  811. --------------------------------------------------
  812. MONTH(datetime_exp) <1>
  813. --------------------------------------------------
  814. *Input*:
  815. <1> date/datetime expression
  816. *Output*: integer
  817. *Description*: Extract the month of the year from a date/datetime.
  818. [source, sql]
  819. --------------------------------------------------
  820. include-tagged::{sql-specs}/docs/docs.csv-spec[monthOfYear]
  821. --------------------------------------------------
  822. [[sql-functions-datetime-monthname]]
  823. ==== `MONTH_NAME/MONTHNAME`
  824. .Synopsis:
  825. [source, sql]
  826. --------------------------------------------------
  827. MONTH_NAME(datetime_exp) <1>
  828. --------------------------------------------------
  829. *Input*:
  830. <1> date/datetime expression
  831. *Output*: string
  832. *Description*: Extract the month from a date/datetime in text format (`January`, `February`...).
  833. [source, sql]
  834. --------------------------------------------------
  835. include-tagged::{sql-specs}/docs/docs.csv-spec[monthName]
  836. --------------------------------------------------
  837. [[sql-functions-now]]
  838. ==== `NOW`
  839. .Synopsis:
  840. [source, sql]
  841. --------------------------------------------------
  842. NOW()
  843. --------------------------------------------------
  844. *Input*: _none_
  845. *Output*: datetime
  846. *Description*: This function offers the same functionality as <<sql-functions-current-timestamp,CURRENT_TIMESTAMP()>> function: returns
  847. the datetime when the current query reached the server. This method always returns the same value for its every
  848. occurrence within the same query.
  849. [source, sql]
  850. --------------------------------------------------
  851. include-tagged::{sql-specs}/docs/docs.csv-spec[nowFunction]
  852. --------------------------------------------------
  853. Typically, this function (as well as its twin <<sql-functions-current-timestamp,CURRENT_TIMESTAMP())>> function is used
  854. for relative date/time filtering:
  855. [source, sql]
  856. --------------------------------------------------
  857. include-tagged::{sql-specs}/docs/docs.csv-spec[filterNow]
  858. --------------------------------------------------
  859. [[sql-functions-datetime-second]]
  860. ==== `SECOND_OF_MINUTE/SECOND`
  861. .Synopsis:
  862. [source, sql]
  863. --------------------------------------------------
  864. SECOND_OF_MINUTE(datetime_exp) <1>
  865. --------------------------------------------------
  866. *Input*:
  867. <1> date/datetime expression
  868. *Output*: integer
  869. *Description*: Extract the second of the minute from a date/datetime.
  870. [source, sql]
  871. --------------------------------------------------
  872. include-tagged::{sql-specs}/docs/docs.csv-spec[secondOfMinute]
  873. --------------------------------------------------
  874. [[sql-functions-datetime-quarter]]
  875. ==== `QUARTER`
  876. .Synopsis:
  877. [source, sql]
  878. --------------------------------------------------
  879. QUARTER(datetime_exp) <1>
  880. --------------------------------------------------
  881. *Input*:
  882. <1> date/datetime expression
  883. *Output*: integer
  884. *Description*: Extract the year quarter the date/datetime falls in.
  885. [source, sql]
  886. --------------------------------------------------
  887. include-tagged::{sql-specs}/docs/docs.csv-spec[quarter]
  888. --------------------------------------------------
  889. [[sql-functions-today]]
  890. ==== `TODAY`
  891. .Synopsis:
  892. [source, sql]
  893. --------------------------------------------------
  894. TODAY()
  895. --------------------------------------------------
  896. *Input*: _none_
  897. *Output*: date
  898. *Description*: This function offers the same functionality as <<sql-functions-current-date,CURRENT_DATE()>> function: returns
  899. the date when the current query reached the server. This method always returns the same value for its every occurrence
  900. within the same query.
  901. [source, sql]
  902. --------------------------------------------------
  903. include-tagged::{sql-specs}/docs/docs.csv-spec[todayFunction]
  904. --------------------------------------------------
  905. Typically, this function (as well as its twin <<sql-functions-current-timestamp,CURRENT_TIMESTAMP())>> function is used
  906. for relative date filtering:
  907. [source, sql]
  908. --------------------------------------------------
  909. include-tagged::{sql-specs}/docs/docs.csv-spec[filterToday]
  910. --------------------------------------------------
  911. [[sql-functions-datetime-week]]
  912. ==== `WEEK_OF_YEAR/WEEK`
  913. .Synopsis:
  914. [source, sql]
  915. --------------------------------------------------
  916. WEEK_OF_YEAR(datetime_exp) <1>
  917. --------------------------------------------------
  918. *Input*:
  919. <1> date/datetime expression
  920. *Output*: integer
  921. *Description*: Extract the week of the year from a date/datetime.
  922. [source, sql]
  923. --------------------------------------------------
  924. include-tagged::{sql-specs}/docs/docs.csv-spec[weekOfYear]
  925. --------------------------------------------------
  926. [[sql-functions-datetime-year]]
  927. ==== `YEAR`
  928. .Synopsis:
  929. [source, sql]
  930. --------------------------------------------------
  931. YEAR(datetime_exp) <1>
  932. --------------------------------------------------
  933. *Input*:
  934. <1> date/datetime expression
  935. *Output*: integer
  936. *Description*: Extract the year from a date/datetime.
  937. [source, sql]
  938. --------------------------------------------------
  939. include-tagged::{sql-specs}/docs/docs.csv-spec[year]
  940. --------------------------------------------------
  941. [[sql-functions-datetime-extract]]
  942. ==== `EXTRACT`
  943. .Synopsis:
  944. [source, sql]
  945. --------------------------------------------------
  946. EXTRACT(
  947. datetime_function <1>
  948. FROM datetime_exp) <2>
  949. --------------------------------------------------
  950. *Input*:
  951. <1> date/time function name
  952. <2> date/datetime expression
  953. *Output*: integer
  954. *Description*: Extract fields from a date/datetime by specifying the name of a <<sql-functions-datetime,datetime function>>.
  955. The following
  956. [source, sql]
  957. --------------------------------------------------
  958. include-tagged::{sql-specs}/docs/docs.csv-spec[extractDayOfYear]
  959. --------------------------------------------------
  960. is the equivalent to
  961. [source, sql]
  962. --------------------------------------------------
  963. include-tagged::{sql-specs}/docs/docs.csv-spec[dayOfYear]
  964. --------------------------------------------------