date-time.asciidoc 41 KB

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