date-time.asciidoc 43 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352
  1. [role="xpack"]
  2. [[sql-functions-datetime]]
  3. === Date/Time and Interval Functions and Operators
  4. {es-sql} offers a wide range of facilities for performing date/time manipulations.
  5. [[sql-functions-datetime-interval]]
  6. ==== Intervals
  7. A common requirement when dealing with date/time in general revolves around
  8. the notion of `interval`, a topic that is worth exploring in the context of {es} and {es-sql}.
  9. {es} has comprehensive support for <<date-math, date math>> both inside <<date-math-index-names, index names>> and <<mapping-date-format, queries>>.
  10. 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`.
  11. The table below shows the mapping between {es} and {es-sql}:
  12. [cols="^m,^m"]
  13. |==========================
  14. s|{es}
  15. s|{es-sql}
  16. 2+h| Index/Table datetime math
  17. 2+|<index-{now/M{YYYY.MM}}>
  18. 2+h| Query date/time math
  19. | 1y | INTERVAL 1 YEAR
  20. | 2M | INTERVAL 2 MONTH
  21. | 3w | INTERVAL 21 DAY
  22. | 4d | INTERVAL 4 DAY
  23. | 5h | INTERVAL 5 HOUR
  24. | 6m | INTERVAL 6 MINUTE
  25. | 7s | INTERVAL 7 SECOND
  26. |==========================
  27. `INTERVAL` allows either `YEAR` and `MONTH` to be mixed together _or_ `DAY`, `HOUR`, `MINUTE` and `SECOND`.
  28. TIP: {es-sql} accepts also the plural for each time unit (e.g. both `YEAR` and `YEARS` are valid).
  29. Example of the possible combinations below:
  30. [cols="^,^"]
  31. |===
  32. s|Interval
  33. s|Description
  34. | `INTERVAL '1-2' YEAR TO MONTH` | 1 year and 2 months
  35. | `INTERVAL '3 4' DAYS TO HOURS` | 3 days and 4 hours
  36. | `INTERVAL '5 6:12' DAYS TO MINUTES` | 5 days, 6 hours and 12 minutes
  37. | `INTERVAL '3 4:56:01' DAY TO SECOND` | 3 days, 4 hours, 56 minutes and 1 second
  38. | `INTERVAL '2 3:45:01.23456789' DAY TO SECOND` | 2 days, 3 hours, 45 minutes, 1 second and 234567890 nanoseconds
  39. | `INTERVAL '123:45' HOUR TO MINUTES` | 123 hours and 45 minutes
  40. | `INTERVAL '65:43:21.0123' HOUR TO SECONDS` | 65 hours, 43 minutes, 21 seconds and 12300000 nanoseconds
  41. | `INTERVAL '45:01.23' MINUTES TO SECONDS` | 45 minutes, 1 second and 230000000 nanoseconds
  42. |===
  43. ==== Comparison
  44. Date/time fields can be compared to <<date-math, date math>> expressions with the equality (`=`) and `IN` operators:
  45. [source, sql]
  46. --------------------------------------------------
  47. include-tagged::{sql-specs}/docs/docs.csv-spec[dtDateMathEquals]
  48. --------------------------------------------------
  49. [source, sql]
  50. --------------------------------------------------
  51. include-tagged::{sql-specs}/docs/docs.csv-spec[dtDateMathIn]
  52. --------------------------------------------------
  53. ==== Operators
  54. Basic arithmetic operators (`+`, `-`, `*`) support date/time parameters as indicated below:
  55. [source, sql]
  56. --------------------------------------------------
  57. include-tagged::{sql-specs}/docs/docs.csv-spec[dtIntervalPlusInterval]
  58. --------------------------------------------------
  59. [source, sql]
  60. --------------------------------------------------
  61. include-tagged::{sql-specs}/docs/docs.csv-spec[dtDateTimePlusInterval]
  62. --------------------------------------------------
  63. [source, sql]
  64. --------------------------------------------------
  65. include-tagged::{sql-specs}/docs/docs.csv-spec[dtMinusInterval]
  66. --------------------------------------------------
  67. [source, sql]
  68. --------------------------------------------------
  69. include-tagged::{sql-specs}/docs/docs.csv-spec[dtIntervalMinusInterval]
  70. --------------------------------------------------
  71. [source, sql]
  72. --------------------------------------------------
  73. include-tagged::{sql-specs}/docs/docs.csv-spec[dtDateTimeMinusInterval]
  74. --------------------------------------------------
  75. [source, sql]
  76. --------------------------------------------------
  77. include-tagged::{sql-specs}/docs/docs.csv-spec[dtIntervalMul]
  78. --------------------------------------------------
  79. ==== Functions
  80. Functions that target date/time.
  81. [[sql-functions-current-date]]
  82. ==== `CURRENT_DATE/CURDATE`
  83. .Synopsis:
  84. [source, sql]
  85. --------------------------------------------------
  86. CURRENT_DATE
  87. CURRENT_DATE()
  88. CURDATE()
  89. --------------------------------------------------
  90. *Input*: _none_
  91. *Output*: date
  92. *Description*: Returns the date (no time part) when the current query reached the server.
  93. It can be used both as a keyword: `CURRENT_DATE` or as a function with no arguments: `CURRENT_DATE()`.
  94. [NOTE]
  95. Unlike CURRENT_DATE, `CURDATE()` can only be used as a function with no arguments and not as a keyword.
  96. This method always returns the same value for its every occurrence within the same query.
  97. [source, sql]
  98. --------------------------------------------------
  99. include-tagged::{sql-specs}/docs/docs.csv-spec[currentDate]
  100. --------------------------------------------------
  101. [source, sql]
  102. --------------------------------------------------
  103. include-tagged::{sql-specs}/docs/docs.csv-spec[currentDateFunction]
  104. --------------------------------------------------
  105. [source, sql]
  106. --------------------------------------------------
  107. include-tagged::{sql-specs}/docs/docs.csv-spec[curDateFunction]
  108. --------------------------------------------------
  109. Typically, this function (as well as its twin <<sql-functions-today,TODAY())>> function
  110. is used for relative date filtering:
  111. [source, sql]
  112. --------------------------------------------------
  113. include-tagged::{sql-specs}/docs/docs.csv-spec[filterToday]
  114. --------------------------------------------------
  115. [[sql-functions-current-time]]
  116. ==== `CURRENT_TIME/CURTIME`
  117. .Synopsis:
  118. [source, sql]
  119. --------------------------------------------------
  120. CURRENT_TIME
  121. CURRENT_TIME([precision]) <1>
  122. CURTIME
  123. --------------------------------------------------
  124. *Input*:
  125. <1> fractional digits; optional
  126. *Output*: time
  127. *Description*: Returns the time when the current query reached the server.
  128. As a function, `CURRENT_TIME()` accepts _precision_ as an optional
  129. parameter for rounding the second fractional digits (nanoseconds). The default _precision_ is 3,
  130. meaning a milliseconds precision current time will be returned.
  131. This method always returns the same value for its every occurrence within the same query.
  132. [source, sql]
  133. --------------------------------------------------
  134. include-tagged::{sql-specs}/docs/docs.csv-spec[currentTime]
  135. --------------------------------------------------
  136. [source, sql]
  137. --------------------------------------------------
  138. include-tagged::{sql-specs}/docs/docs.csv-spec[currentTimeFunction]
  139. --------------------------------------------------
  140. [source, sql]
  141. --------------------------------------------------
  142. include-tagged::{sql-specs}/docs/docs.csv-spec[curTimeFunction]
  143. --------------------------------------------------
  144. [source, sql]
  145. --------------------------------------------------
  146. include-tagged::{sql-specs}/docs/docs.csv-spec[currentTimeFunctionPrecision]
  147. --------------------------------------------------
  148. Typically, this function is used for relative date/time filtering:
  149. [source, sql]
  150. --------------------------------------------------
  151. include-tagged::{sql-specs}/docs/docs.csv-spec[filterCurrentTime]
  152. --------------------------------------------------
  153. [IMPORTANT]
  154. Currently, using a _precision_ greater than 6 doesn't make any difference to the output of the
  155. function as the maximum number of second fractional digits returned is 6.
  156. [[sql-functions-current-timestamp]]
  157. ==== `CURRENT_TIMESTAMP`
  158. .Synopsis:
  159. [source, sql]
  160. --------------------------------------------------
  161. CURRENT_TIMESTAMP
  162. CURRENT_TIMESTAMP([precision]) <1>
  163. --------------------------------------------------
  164. *Input*:
  165. <1> fractional digits; optional
  166. *Output*: date/time
  167. *Description*: Returns the date/time when the current query reached the server.
  168. As a function, `CURRENT_TIMESTAMP()` accepts _precision_ as an optional
  169. parameter for rounding the second fractional digits (nanoseconds). The default _precision_ is 3,
  170. meaning a milliseconds precision current date/time will be returned.
  171. This method always returns the same value for its every occurrence within the same query.
  172. [source, sql]
  173. --------------------------------------------------
  174. include-tagged::{sql-specs}/docs/docs.csv-spec[curTs]
  175. --------------------------------------------------
  176. [source, sql]
  177. --------------------------------------------------
  178. include-tagged::{sql-specs}/docs/docs.csv-spec[curTsFunction]
  179. --------------------------------------------------
  180. [source, sql]
  181. --------------------------------------------------
  182. include-tagged::{sql-specs}/docs/docs.csv-spec[curTsFunctionPrecision]
  183. --------------------------------------------------
  184. Typically, this function (as well as its twin <<sql-functions-now,NOW())>> function is used for
  185. relative date/time filtering:
  186. [source, sql]
  187. --------------------------------------------------
  188. include-tagged::{sql-specs}/docs/docs.csv-spec[filterNow]
  189. --------------------------------------------------
  190. [IMPORTANT]
  191. Currently, using a _precision_ greater than 6 doesn't make any difference to the output of the
  192. function as the maximum number of second fractional digits returned is 6.
  193. [[sql-functions-datetime-add]]
  194. ==== `DATE_ADD/DATEADD/TIMESTAMP_ADD/TIMESTAMPADD`
  195. .Synopsis:
  196. [source, sql]
  197. --------------------------------------------------
  198. DATE_ADD(
  199. string_exp, <1>
  200. integer_exp, <2>
  201. datetime_exp) <3>
  202. --------------------------------------------------
  203. *Input*:
  204. <1> string expression denoting the date/time unit to add to the date/datetime.
  205. If `null`, the function returns `null`.
  206. <2> integer expression denoting how many times the above unit should be added
  207. to/from the date/datetime, if a negative value is used it results to a
  208. subtraction from the date/datetime. If `null`, the function returns `null`.
  209. <3> date/datetime expression. If `null`, the function returns `null`.
  210. *Output*: datetime
  211. *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
  212. the date/datetime.
  213. [WARNING]
  214. If the second argument is a long there is possibility of truncation since an integer value will be extracted and
  215. used from that long.
  216. [cols="^,^"]
  217. |===
  218. 2+h|Datetime units to add/subtract
  219. s|unit
  220. s|abbreviations
  221. | year | years, yy, yyyy
  222. | quarter | quarters, qq, q
  223. | month | months, mm, m
  224. | dayofyear | dy, y
  225. | day | days, dd, d
  226. | week | weeks, wk, ww
  227. | weekday | weekdays, dw
  228. | hour | hours, hh
  229. | minute | minutes, mi, n
  230. | second | seconds, ss, s
  231. | millisecond | milliseconds, ms
  232. | microsecond | microseconds, mcs
  233. | nanosecond | nanoseconds, ns
  234. |===
  235. [source, sql]
  236. --------------------------------------------------
  237. include-tagged::{sql-specs}/docs/docs.csv-spec[dateAddDateTimeYears]
  238. --------------------------------------------------
  239. [source, sql]
  240. --------------------------------------------------
  241. include-tagged::{sql-specs}/docs/docs.csv-spec[dateAddDateTimeWeeks]
  242. --------------------------------------------------
  243. [source, sql]
  244. --------------------------------------------------
  245. include-tagged::{sql-specs}/docs/docs.csv-spec[dateAddDateTimeSeconds]
  246. --------------------------------------------------
  247. [source, sql]
  248. --------------------------------------------------
  249. include-tagged::{sql-specs}/docs/docs.csv-spec[dateAddDateQuarters]
  250. --------------------------------------------------
  251. [source, sql]
  252. --------------------------------------------------
  253. include-tagged::{sql-specs}/docs/docs.csv-spec[dateAddDateMinutes]
  254. --------------------------------------------------
  255. [[sql-functions-datetime-diff]]
  256. ==== `DATE_DIFF/DATEDIFF/TIMESTAMP_DIFF/TIMESTAMPDIFF`
  257. .Synopsis:
  258. [source, sql]
  259. --------------------------------------------------
  260. DATE_DIFF(
  261. string_exp, <1>
  262. datetime_exp, <2>
  263. datetime_exp) <3>
  264. --------------------------------------------------
  265. *Input*:
  266. <1> string expression denoting the date/time unit difference between the
  267. following two date/datetime expressions. If `null`, the function returns `null`.
  268. <2> start date/datetime expression. If `null`, the function returns `null`.
  269. <3> end date/datetime expression. If `null`, the function returns `null`.
  270. *Output*: integer
  271. *Description*: Subtract the second argument from the third argument and return their difference in multiples of the unit
  272. specified in the first argument. If the second argument (start) is greater than the third argument (end),
  273. then negative values are returned.
  274. [cols="^,^"]
  275. |===
  276. 2+h|Datetime difference units
  277. s|unit
  278. s|abbreviations
  279. | year | years, yy, yyyy
  280. | quarter | quarters, qq, q
  281. | month | months, mm, m
  282. | dayofyear | dy, y
  283. | day | days, dd, d
  284. | week | weeks, wk, ww
  285. | weekday | weekdays, dw
  286. | hour | hours, hh
  287. | minute | minutes, mi, n
  288. | second | seconds, ss, s
  289. | millisecond | milliseconds, ms
  290. | microsecond | microseconds, mcs
  291. | nanosecond | nanoseconds, ns
  292. |===
  293. [source, sql]
  294. --------------------------------------------------
  295. include-tagged::{sql-specs}/docs/docs.csv-spec[dateDiffDateTimeYears]
  296. --------------------------------------------------
  297. [source, sql]
  298. --------------------------------------------------
  299. include-tagged::{sql-specs}/docs/docs.csv-spec[dateDiffDateTimeWeeks]
  300. --------------------------------------------------
  301. [source, sql]
  302. --------------------------------------------------
  303. include-tagged::{sql-specs}/docs/docs.csv-spec[dateDiffDateTimeSeconds]
  304. --------------------------------------------------
  305. [source, sql]
  306. --------------------------------------------------
  307. include-tagged::{sql-specs}/docs/docs.csv-spec[dateDiffDateQuarters]
  308. --------------------------------------------------
  309. [NOTE]
  310. For `hour` and `minute`, `DATEDIFF` doesn't do any rounding, but instead first truncates
  311. the more detailed time fields on the 2 dates to zero and then calculates the subtraction.
  312. [source, sql]
  313. --------------------------------------------------
  314. include-tagged::{sql-specs}/docs/docs.csv-spec[dateDiffDateTimeHours]
  315. --------------------------------------------------
  316. [source, sql]
  317. --------------------------------------------------
  318. include-tagged::{sql-specs}/docs/docs.csv-spec[dateDiffDateTimeMinutes]
  319. --------------------------------------------------
  320. [source, sql]
  321. --------------------------------------------------
  322. include-tagged::{sql-specs}/docs/docs.csv-spec[dateDiffDateMinutes]
  323. --------------------------------------------------
  324. [[sql-functions-datetime-dateformat]]
  325. ==== `DATE_FORMAT`
  326. .Synopsis:
  327. [source, sql]
  328. --------------------------------------------------
  329. DATE_FORMAT(
  330. date_exp/datetime_exp/time_exp, <1>
  331. string_exp) <2>
  332. --------------------------------------------------
  333. *Input*:
  334. <1> date/datetime/time expression. If `null`, the function returns `null`.
  335. <2> format pattern. If `null` or an empty string, the function returns `null`.
  336. *Output*: string
  337. *Description*: Returns the date/datetime/time as a string using the format specified in the 2nd argument. The formatting
  338. pattern is one of the specifiers used in the
  339. https://dev.mysql.com/doc/refman/8.0/en/date-and-time-functions.html#function_date-format[MySQL DATE_FORMAT() function].
  340. [NOTE]
  341. If the 1st argument is of type `time`, then pattern specified by the 2nd argument cannot contain date related units
  342. (e.g. 'dd', 'MM', 'yyyy', etc.). If it contains such units an error is returned.
  343. Ranges for month and day specifiers (%c, %D, %d, %e, %m) start at one, unlike MySQL, where they start at zero, due to
  344. the fact that MySQL permits the storing of incomplete dates such as '2014-00-00'.
  345. Elasticsearch in this case returns an error.
  346. [source, sql]
  347. --------------------------------------------------
  348. include-tagged::{sql-specs}/docs/docs.csv-spec[dateFormatDate]
  349. --------------------------------------------------
  350. [source, sql]
  351. --------------------------------------------------
  352. include-tagged::{sql-specs}/docs/docs.csv-spec[dateFormatDateTime]
  353. --------------------------------------------------
  354. [source, sql]
  355. --------------------------------------------------
  356. include-tagged::{sql-specs}/docs/docs.csv-spec[dateFormatTime]
  357. --------------------------------------------------
  358. [[sql-functions-datetime-dateparse]]
  359. ==== `DATE_PARSE`
  360. .Synopsis:
  361. [source, sql]
  362. --------------------------------------------------
  363. DATE_PARSE(
  364. string_exp, <1>
  365. string_exp) <2>
  366. --------------------------------------------------
  367. *Input*:
  368. <1> date expression as a string. If `null` or an empty string, the function
  369. returns `null`.
  370. <2> parsing pattern. If `null` or an empty string, the function returns `null`.
  371. *Output*: date
  372. *Description*: Returns a date by parsing the 1st argument using the format specified in the 2nd argument. The parsing
  373. format pattern used is the one from
  374. https://docs.oracle.com/en/java/javase/14/docs/api/java.base/java/time/format/DateTimeFormatter.html[`java.time.format.DateTimeFormatter`].
  375. [NOTE]
  376. 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
  377. as the function needs to return a value of `date` type which will contain date part.
  378. [source, sql]
  379. --------------------------------------------------
  380. include-tagged::{sql-specs}/docs/docs.csv-spec[dateParse1]
  381. --------------------------------------------------
  382. [NOTE]
  383. ====
  384. The resulting `date` will have the time zone specified by the user through the
  385. <<sql-search-api-time-zone,`time_zone`>>/<<jdbc-cfg-timezone,`timezone`>> REST/driver parameters
  386. with no conversion applied.
  387. [source, sql]
  388. --------------------------------------------------
  389. include-tagged::{sql-specs}/docs/docs.csv-spec[dateParse2]
  390. --------------------------------------------------
  391. ====
  392. [[sql-functions-datetime-datetimeformat]]
  393. ==== `DATETIME_FORMAT`
  394. .Synopsis:
  395. [source, sql]
  396. --------------------------------------------------
  397. DATETIME_FORMAT(
  398. date_exp/datetime_exp/time_exp, <1>
  399. string_exp) <2>
  400. --------------------------------------------------
  401. *Input*:
  402. <1> date/datetime/time expression. If `null`, the function returns `null`.
  403. <2> format pattern. If `null` or an empty string, the function returns `null`.
  404. *Output*: string
  405. *Description*: Returns the date/datetime/time as a string using the format specified in the 2nd argument. The formatting
  406. pattern used is the one from
  407. https://docs.oracle.com/en/java/javase/14/docs/api/java.base/java/time/format/DateTimeFormatter.html[`java.time.format.DateTimeFormatter`].
  408. [NOTE]
  409. If the 1st argument is of type `time`, then pattern specified by the 2nd argument cannot contain date related units
  410. (e.g. 'dd', 'MM', 'yyyy', etc.). If it contains such units an error is returned.
  411. [source, sql]
  412. --------------------------------------------------
  413. include-tagged::{sql-specs}/docs/docs.csv-spec[dateTimeFormatDate]
  414. --------------------------------------------------
  415. [source, sql]
  416. --------------------------------------------------
  417. include-tagged::{sql-specs}/docs/docs.csv-spec[dateTimeFormatDateTime]
  418. --------------------------------------------------
  419. [source, sql]
  420. --------------------------------------------------
  421. include-tagged::{sql-specs}/docs/docs.csv-spec[dateTimeFormatTime]
  422. --------------------------------------------------
  423. [[sql-functions-datetime-datetimeparse]]
  424. ==== `DATETIME_PARSE`
  425. .Synopsis:
  426. [source, sql]
  427. --------------------------------------------------
  428. DATETIME_PARSE(
  429. string_exp, <1>
  430. string_exp) <2>
  431. --------------------------------------------------
  432. *Input*:
  433. <1> datetime expression as a string. If `null` or an empty string, the function
  434. returns `null`.
  435. <2> parsing pattern. If `null` or an empty string, the function returns `null`.
  436. *Output*: datetime
  437. *Description*: Returns a datetime by parsing the 1st argument using the format specified in the 2nd argument. The parsing
  438. format pattern used is the one from
  439. https://docs.oracle.com/en/java/javase/14/docs/api/java.base/java/time/format/DateTimeFormatter.html[`java.time.format.DateTimeFormatter`].
  440. [NOTE]
  441. If the parsing pattern contains only date or only time units (e.g. 'dd/MM/yyyy', 'HH:mm:ss', etc.) an error is returned
  442. as the function needs to return a value of `datetime` type which must contain both.
  443. [source, sql]
  444. --------------------------------------------------
  445. include-tagged::{sql-specs}/docs/docs.csv-spec[dateTimeParse1]
  446. --------------------------------------------------
  447. [source, sql]
  448. --------------------------------------------------
  449. include-tagged::{sql-specs}/docs/docs.csv-spec[dateTimeParse2]
  450. --------------------------------------------------
  451. [NOTE]
  452. ====
  453. If timezone is not specified in the datetime string expression and the parsing pattern, the resulting `datetime` will have the
  454. time zone specified by the user through the <<sql-search-api-time-zone,`time_zone`>>/<<jdbc-cfg-timezone,`timezone`>> REST/driver parameters
  455. with no conversion applied.
  456. [source, sql]
  457. --------------------------------------------------
  458. include-tagged::{sql-specs}/docs/docs.csv-spec[dateTimeParse3]
  459. --------------------------------------------------
  460. ====
  461. [[sql-functions-datetime-timeparse]]
  462. ==== `TIME_PARSE`
  463. .Synopsis:
  464. [source, sql]
  465. --------------------------------------------------
  466. TIME_PARSE(
  467. string_exp, <1>
  468. string_exp) <2>
  469. --------------------------------------------------
  470. *Input*:
  471. <1> time expression as a string. If `null` or an empty string, the function
  472. returns `null`.
  473. <2> parsing pattern. If `null` or an empty string, the function returns `null`.
  474. *Output*: time
  475. *Description*: Returns a time by parsing the 1st argument using the format specified in the 2nd argument. The parsing
  476. format pattern used is the one from
  477. https://docs.oracle.com/en/java/javase/14/docs/api/java.base/java/time/format/DateTimeFormatter.html[`java.time.format.DateTimeFormatter`].
  478. [NOTE]
  479. If the parsing pattern contains only date units (e.g. 'dd/MM/yyyy') an error is returned
  480. as the function needs to return a value of `time` type which will contain only time.
  481. [source, sql]
  482. --------------------------------------------------
  483. include-tagged::{sql-specs}/docs/docs.csv-spec[timeParse1]
  484. --------------------------------------------------
  485. [source, sql]
  486. --------------------------------------------------
  487. include-tagged::{sql-specs}/docs/docs.csv-spec[timeParse2]
  488. --------------------------------------------------
  489. [NOTE]
  490. ====
  491. If timezone is not specified in the time string expression and the parsing pattern,
  492. the resulting `time` will have the offset of the time zone specified by the user through the
  493. <<sql-search-api-time-zone,`time_zone`>>/<<jdbc-cfg-timezone,`timezone`>> REST/driver
  494. parameters at the Unix epoch date (`1970-01-01`) with no conversion applied.
  495. [source, sql]
  496. --------------------------------------------------
  497. include-tagged::{sql-specs}/docs/docs.csv-spec[timeParse3]
  498. --------------------------------------------------
  499. ====
  500. [[sql-functions-datetime-part]]
  501. ==== `DATE_PART/DATEPART`
  502. .Synopsis:
  503. [source, sql]
  504. --------------------------------------------------
  505. DATE_PART(
  506. string_exp, <1>
  507. datetime_exp) <2>
  508. --------------------------------------------------
  509. *Input*:
  510. <1> string expression denoting the unit to extract from the date/datetime. If
  511. `null`, the function returns `null`.
  512. <2> date/datetime expression. If `null`, the function returns `null`.
  513. *Output*: integer
  514. *Description*: Extract the specified unit from a date/datetime. It's similar to
  515. <<sql-functions-datetime-extract>> but with different names and aliases for the
  516. units and provides more options (e.g.: `TZOFFSET`).
  517. [cols="^,^"]
  518. |===
  519. 2+h|Datetime units to extract
  520. s|unit
  521. s|abbreviations
  522. | year | years, yy, yyyy
  523. | quarter | quarters, qq, q
  524. | month | months, mm, m
  525. | dayofyear | dy, y
  526. | day | days, dd, d
  527. | week | weeks, wk, ww
  528. | weekday | weekdays, dw
  529. | hour | hours, hh
  530. | minute | minutes, mi, n
  531. | second | seconds, ss, s
  532. | millisecond | milliseconds, ms
  533. | microsecond | microseconds, mcs
  534. | nanosecond | nanoseconds, ns
  535. | tzoffset | tz
  536. |===
  537. [source, sql]
  538. --------------------------------------------------
  539. include-tagged::{sql-specs}/docs/docs.csv-spec[datePartDateTimeYears]
  540. --------------------------------------------------
  541. [source, sql]
  542. --------------------------------------------------
  543. include-tagged::{sql-specs}/docs/docs.csv-spec[datePartDateTimeMinutes]
  544. --------------------------------------------------
  545. [source, sql]
  546. --------------------------------------------------
  547. include-tagged::{sql-specs}/docs/docs.csv-spec[datePartDateQuarter]
  548. --------------------------------------------------
  549. [source, sql]
  550. --------------------------------------------------
  551. include-tagged::{sql-specs}/docs/docs.csv-spec[datePartDateMonth]
  552. --------------------------------------------------
  553. [NOTE]
  554. For `week` and `weekday` the unit is extracted using the non-ISO calculation, which means
  555. that a given week is considered to start from Sunday, not Monday.
  556. [source, sql]
  557. --------------------------------------------------
  558. include-tagged::{sql-specs}/docs/docs.csv-spec[datePartDateTimeWeek]
  559. --------------------------------------------------
  560. [NOTE]
  561. The `tzoffset` returns the total number of minutes (signed) that represent the time zone's offset.
  562. [source, sql]
  563. --------------------------------------------------
  564. include-tagged::{sql-specs}/docs/docs.csv-spec[datePartDateTimeTzOffsetPlus]
  565. --------------------------------------------------
  566. [source, sql]
  567. --------------------------------------------------
  568. include-tagged::{sql-specs}/docs/docs.csv-spec[datePartDateTimeTzOffsetMinus]
  569. --------------------------------------------------
  570. [[sql-functions-datetime-trunc]]
  571. ==== `DATE_TRUNC/DATETRUNC`
  572. .Synopsis:
  573. [source, sql]
  574. --------------------------------------------------
  575. DATE_TRUNC(
  576. string_exp, <1>
  577. datetime_exp/interval_exp) <2>
  578. --------------------------------------------------
  579. *Input*:
  580. <1> string expression denoting the unit to which the date/datetime/interval
  581. should be truncated to. If `null`, the function returns `null`.
  582. <2> date/datetime/interval expression. If `null`, the function returns `null`.
  583. *Output*: datetime/interval
  584. *Description*: Truncate the date/datetime/interval to the specified unit by
  585. setting all fields that are less significant than the specified one to zero (or
  586. one, for day, day of week and month). If the first argument is `week` and the
  587. second argument is of `interval` type, an error is thrown since the `interval`
  588. data type doesn't support a `week` time unit.
  589. [cols="^,^"]
  590. |===
  591. 2+h|Datetime truncation units
  592. s|unit
  593. s|abbreviations
  594. | millennium | millennia
  595. | century | centuries
  596. | decade | decades
  597. | year | years, yy, yyyy
  598. | quarter | quarters, qq, q
  599. | month | months, mm, m
  600. | week | weeks, wk, ww
  601. | day | days, dd, d
  602. | hour | hours, hh
  603. | minute | minutes, mi, n
  604. | second | seconds, ss, s
  605. | millisecond | milliseconds, ms
  606. | microsecond | microseconds, mcs
  607. | nanosecond | nanoseconds, ns
  608. |===
  609. [source, sql]
  610. --------------------------------------------------
  611. include-tagged::{sql-specs}/docs/docs.csv-spec[truncateDateTimeMillennium]
  612. --------------------------------------------------
  613. [source, sql]
  614. --------------------------------------------------
  615. include-tagged::{sql-specs}/docs/docs.csv-spec[truncateDateTimeWeek]
  616. --------------------------------------------------
  617. [source, sql]
  618. --------------------------------------------------
  619. include-tagged::{sql-specs}/docs/docs.csv-spec[truncateDateTimeMinutes]
  620. --------------------------------------------------
  621. [source, sql]
  622. --------------------------------------------------
  623. include-tagged::{sql-specs}/docs/docs.csv-spec[truncateDateDecades]
  624. --------------------------------------------------
  625. [source, sql]
  626. --------------------------------------------------
  627. include-tagged::{sql-specs}/docs/docs.csv-spec[truncateDateQuarter]
  628. --------------------------------------------------
  629. [source, sql]
  630. --------------------------------------------------
  631. include-tagged::{sql-specs}/docs/docs.csv-spec[truncateIntervalCenturies]
  632. --------------------------------------------------
  633. [source, sql]
  634. --------------------------------------------------
  635. include-tagged::{sql-specs}/docs/docs.csv-spec[truncateIntervalHour]
  636. --------------------------------------------------
  637. [source, sql]
  638. --------------------------------------------------
  639. include-tagged::{sql-specs}/docs/docs.csv-spec[truncateIntervalDay]
  640. --------------------------------------------------
  641. [[sql-functions-datetime-format]]
  642. ==== `FORMAT`
  643. .Synopsis:
  644. [source, sql]
  645. --------------------------------------------------
  646. FORMAT(
  647. date_exp/datetime_exp/time_exp, <1>
  648. string_exp) <2>
  649. --------------------------------------------------
  650. *Input*:
  651. <1> date/datetime/time expression. If `null`, the function returns `null`.
  652. <2> format pattern. If `null` or an empty string, the function returns `null`.
  653. *Output*: string
  654. *Description*: Returns the date/datetime/time as a string using the
  655. https://docs.microsoft.com/en-us/sql/t-sql/functions/format-transact-sql#arguments[format]
  656. specified in the 2nd argument. The formatting pattern used is the one from
  657. https://docs.microsoft.com/en-us/dotnet/standard/base-types/custom-date-and-time-format-strings[Microsoft
  658. SQL Server Format Specification].
  659. [NOTE]
  660. If the 1st argument is of type `time`, then pattern specified by the 2nd argument cannot contain date related units
  661. (e.g. 'dd', 'MM', 'yyyy', etc.). If it contains such units an error is returned. +
  662. Format specifier `F` will be working similar to format specifier `f`.
  663. 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).
  664. Result will contain `0` appended in the end to match with number of `F` provided.
  665. 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`. +
  666. Format specifier `y` will return year-of-era instead of one/two low-order digits.
  667. eg.: For year `2009`, `y` will be returning `2009` instead of `9`. For year `43`, `y` format specifier will return `43`.
  668. - Special characters like `"` , `\` and `%` will be returned as it is without any change. eg.: formatting date `17-sep-2020` with `%M` will return `%9`
  669. [source, sql]
  670. --------------------------------------------------
  671. include-tagged::{sql-specs}/docs/docs.csv-spec[formatDate]
  672. --------------------------------------------------
  673. [source, sql]
  674. --------------------------------------------------
  675. include-tagged::{sql-specs}/docs/docs.csv-spec[formatDateTime]
  676. --------------------------------------------------
  677. [source, sql]
  678. --------------------------------------------------
  679. include-tagged::{sql-specs}/docs/docs.csv-spec[formatTime]
  680. --------------------------------------------------
  681. [[sql-functions-datetime-to_char]]
  682. ==== `TO_CHAR`
  683. .Synopsis:
  684. [source, sql]
  685. --------------------------------------------------
  686. TO_CHAR(
  687. date_exp/datetime_exp/time_exp, <1>
  688. string_exp) <2>
  689. --------------------------------------------------
  690. *Input*:
  691. <1> date/datetime/time expression. If `null`, the function returns `null`.
  692. <2> format pattern. If `null` or an empty string, the function returns `null`.
  693. *Output*: string
  694. *Description*: Returns the date/datetime/time as a string using the format
  695. specified in the 2nd argument. The formatting pattern conforms to
  696. https://www.postgresql.org/docs/13/functions-formatting.html[PostgreSQL Template
  697. Patterns for Date/Time Formatting].
  698. [NOTE]
  699. If the 1st argument is of type `time`, then the pattern specified by the 2nd argument cannot contain date related units
  700. (e.g. 'dd', 'MM', 'YYYY', etc.). If it contains such units an error is returned. +
  701. The result of the patterns `TZ` and `tz` (time zone abbreviations) in some cases differ from the results returned by the `TO_CHAR`
  702. in PostgreSQL. The reason is that the time zone abbreviations specified by the JDK are different from the ones specified by PostgreSQL.
  703. This function might show an actual time zone abbreviation instead of the generic `LMT` or empty string or offset returned by the PostgreSQL
  704. implementation. The summer/daylight markers might also differ between the two implementations (e.g. will show `HT` instead of `HST`
  705. for Hawaii). +
  706. The `FX`, `TM`, `SP` pattern modifiers are not supported and will show up as `FX`, `TM`, `SP` literals in the output.
  707. [source, sql]
  708. --------------------------------------------------
  709. include-tagged::{sql-specs}/docs/docs.csv-spec[toCharDate]
  710. --------------------------------------------------
  711. [source, sql]
  712. --------------------------------------------------
  713. include-tagged::{sql-specs}/docs/docs.csv-spec[toCharDateTime]
  714. --------------------------------------------------
  715. [source, sql]
  716. --------------------------------------------------
  717. include-tagged::{sql-specs}/docs/docs.csv-spec[toCharTime]
  718. --------------------------------------------------
  719. [[sql-functions-datetime-day]]
  720. ==== `DAY_OF_MONTH/DOM/DAY`
  721. .Synopsis:
  722. [source, sql]
  723. --------------------------------------------------
  724. DAY_OF_MONTH(datetime_exp) <1>
  725. --------------------------------------------------
  726. *Input*:
  727. <1> date/datetime expression. If `null`, the function returns `null`.
  728. *Output*: integer
  729. *Description*: Extract the day of the month from a date/datetime.
  730. [source, sql]
  731. --------------------------------------------------
  732. include-tagged::{sql-specs}/docs/docs.csv-spec[dayOfMonth]
  733. --------------------------------------------------
  734. [[sql-functions-datetime-dow]]
  735. ==== `DAY_OF_WEEK/DAYOFWEEK/DOW`
  736. .Synopsis:
  737. [source, sql]
  738. --------------------------------------------------
  739. DAY_OF_WEEK(datetime_exp) <1>
  740. --------------------------------------------------
  741. *Input*:
  742. <1> date/datetime expression. If `null`, the function returns `null`.
  743. *Output*: integer
  744. *Description*: Extract the day of the week from a date/datetime. Sunday is `1`, Monday is `2`, etc.
  745. [source, sql]
  746. --------------------------------------------------
  747. include-tagged::{sql-specs}/docs/docs.csv-spec[dayOfWeek]
  748. --------------------------------------------------
  749. [[sql-functions-datetime-doy]]
  750. ==== `DAY_OF_YEAR/DOY`
  751. .Synopsis:
  752. [source, sql]
  753. --------------------------------------------------
  754. DAY_OF_YEAR(datetime_exp) <1>
  755. --------------------------------------------------
  756. *Input*:
  757. <1> date/datetime expression. If `null`, the function returns `null`.
  758. *Output*: integer
  759. *Description*: Extract the day of the year from a date/datetime.
  760. [source, sql]
  761. --------------------------------------------------
  762. include-tagged::{sql-specs}/docs/docs.csv-spec[dayOfYear]
  763. --------------------------------------------------
  764. [[sql-functions-datetime-dayname]]
  765. ==== `DAY_NAME/DAYNAME`
  766. .Synopsis:
  767. [source, sql]
  768. --------------------------------------------------
  769. DAY_NAME(datetime_exp) <1>
  770. --------------------------------------------------
  771. *Input*:
  772. <1> date/datetime expression. If `null`, the function returns `null`.
  773. *Output*: string
  774. *Description*: Extract the day of the week from a date/datetime in text format (`Monday`, `Tuesday`...).
  775. [source, sql]
  776. --------------------------------------------------
  777. include-tagged::{sql-specs}/docs/docs.csv-spec[dayName]
  778. --------------------------------------------------
  779. [[sql-functions-datetime-hour]]
  780. ==== `HOUR_OF_DAY/HOUR`
  781. .Synopsis:
  782. [source, sql]
  783. --------------------------------------------------
  784. HOUR_OF_DAY(datetime_exp) <1>
  785. --------------------------------------------------
  786. *Input*:
  787. <1> date/datetime expression. If `null`, the function returns `null`.
  788. *Output*: integer
  789. *Description*: Extract the hour of the day from a date/datetime.
  790. [source, sql]
  791. --------------------------------------------------
  792. include-tagged::{sql-specs}/docs/docs.csv-spec[hourOfDay]
  793. --------------------------------------------------
  794. [[sql-functions-datetime-isodow]]
  795. ==== `ISO_DAY_OF_WEEK/ISODAYOFWEEK/ISODOW/IDOW`
  796. .Synopsis:
  797. [source, sql]
  798. --------------------------------------------------
  799. ISO_DAY_OF_WEEK(datetime_exp) <1>
  800. --------------------------------------------------
  801. *Input*:
  802. <1> date/datetime expression. If `null`, the function returns `null`.
  803. *Output*: integer
  804. *Description*: Extract the day of the week from a date/datetime, following the {wikipedia}/ISO_week_date[ISO 8601 standard].
  805. Monday is `1`, Tuesday is `2`, etc.
  806. [source, sql]
  807. --------------------------------------------------
  808. include-tagged::{sql-specs}/docs/docs.csv-spec[isoDayOfWeek]
  809. --------------------------------------------------
  810. [[sql-functions-datetime-isoweek]]
  811. ==== `ISO_WEEK_OF_YEAR/ISOWEEKOFYEAR/ISOWEEK/IWOY/IW`
  812. .Synopsis:
  813. [source, sql]
  814. --------------------------------------------------
  815. ISO_WEEK_OF_YEAR(datetime_exp) <1>
  816. --------------------------------------------------
  817. *Input*:
  818. <1> date/datetime expression. If `null`, the function returns `null`.
  819. *Output*: integer
  820. *Description*: Extract the week of the year from a date/datetime, following {wikipedia}/ISO_week_date[ISO 8601 standard]. The first week
  821. of a year is the first week with a majority (4 or more) of its days in January.
  822. [source, sql]
  823. --------------------------------------------------
  824. include-tagged::{sql-specs}/docs/docs.csv-spec[isoWeekOfYear]
  825. --------------------------------------------------
  826. [[sql-functions-datetime-minuteofday]]
  827. ==== `MINUTE_OF_DAY`
  828. .Synopsis:
  829. [source, sql]
  830. --------------------------------------------------
  831. MINUTE_OF_DAY(datetime_exp) <1>
  832. --------------------------------------------------
  833. *Input*:
  834. <1> date/datetime expression. If `null`, the function returns `null`.
  835. *Output*: integer
  836. *Description*: Extract the minute of the day from a date/datetime.
  837. [source, sql]
  838. --------------------------------------------------
  839. include-tagged::{sql-specs}/docs/docs.csv-spec[minuteOfDay]
  840. --------------------------------------------------
  841. [[sql-functions-datetime-minute]]
  842. ==== `MINUTE_OF_HOUR/MINUTE`
  843. .Synopsis:
  844. [source, sql]
  845. --------------------------------------------------
  846. MINUTE_OF_HOUR(datetime_exp) <1>
  847. --------------------------------------------------
  848. *Input*:
  849. <1> date/datetime expression. If `null`, the function returns `null`.
  850. *Output*: integer
  851. *Description*: Extract the minute of the hour from a date/datetime.
  852. [source, sql]
  853. --------------------------------------------------
  854. include-tagged::{sql-specs}/docs/docs.csv-spec[minuteOfHour]
  855. --------------------------------------------------
  856. [[sql-functions-datetime-month]]
  857. ==== `MONTH_OF_YEAR/MONTH`
  858. .Synopsis:
  859. [source, sql]
  860. --------------------------------------------------
  861. MONTH(datetime_exp) <1>
  862. --------------------------------------------------
  863. *Input*:
  864. <1> date/datetime expression. If `null`, the function returns `null`.
  865. *Output*: integer
  866. *Description*: Extract the month of the year from a date/datetime.
  867. [source, sql]
  868. --------------------------------------------------
  869. include-tagged::{sql-specs}/docs/docs.csv-spec[monthOfYear]
  870. --------------------------------------------------
  871. [[sql-functions-datetime-monthname]]
  872. ==== `MONTH_NAME/MONTHNAME`
  873. .Synopsis:
  874. [source, sql]
  875. --------------------------------------------------
  876. MONTH_NAME(datetime_exp) <1>
  877. --------------------------------------------------
  878. *Input*:
  879. <1> date/datetime expression. If `null`, the function returns `null`.
  880. *Output*: string
  881. *Description*: Extract the month from a date/datetime in text format (`January`, `February`...).
  882. [source, sql]
  883. --------------------------------------------------
  884. include-tagged::{sql-specs}/docs/docs.csv-spec[monthName]
  885. --------------------------------------------------
  886. [[sql-functions-now]]
  887. ==== `NOW`
  888. .Synopsis:
  889. [source, sql]
  890. --------------------------------------------------
  891. NOW()
  892. --------------------------------------------------
  893. *Input*: _none_
  894. *Output*: datetime
  895. *Description*: This function offers the same functionality as <<sql-functions-current-timestamp,CURRENT_TIMESTAMP()>> function: returns
  896. the datetime when the current query reached the server. This method always returns the same value for its every
  897. occurrence within the same query.
  898. [source, sql]
  899. --------------------------------------------------
  900. include-tagged::{sql-specs}/docs/docs.csv-spec[nowFunction]
  901. --------------------------------------------------
  902. Typically, this function (as well as its twin <<sql-functions-current-timestamp,CURRENT_TIMESTAMP())>> function is used
  903. for relative date/time filtering:
  904. [source, sql]
  905. --------------------------------------------------
  906. include-tagged::{sql-specs}/docs/docs.csv-spec[filterNow]
  907. --------------------------------------------------
  908. [[sql-functions-datetime-second]]
  909. ==== `SECOND_OF_MINUTE/SECOND`
  910. .Synopsis:
  911. [source, sql]
  912. --------------------------------------------------
  913. SECOND_OF_MINUTE(datetime_exp) <1>
  914. --------------------------------------------------
  915. *Input*:
  916. <1> date/datetime expression. If `null`, the function returns `null`.
  917. *Output*: integer
  918. *Description*: Extract the second of the minute from a date/datetime.
  919. [source, sql]
  920. --------------------------------------------------
  921. include-tagged::{sql-specs}/docs/docs.csv-spec[secondOfMinute]
  922. --------------------------------------------------
  923. [[sql-functions-datetime-quarter]]
  924. ==== `QUARTER`
  925. .Synopsis:
  926. [source, sql]
  927. --------------------------------------------------
  928. QUARTER(datetime_exp) <1>
  929. --------------------------------------------------
  930. *Input*:
  931. <1> date/datetime expression. If `null`, the function returns `null`.
  932. *Output*: integer
  933. *Description*: Extract the year quarter the date/datetime falls in.
  934. [source, sql]
  935. --------------------------------------------------
  936. include-tagged::{sql-specs}/docs/docs.csv-spec[quarter]
  937. --------------------------------------------------
  938. [[sql-functions-today]]
  939. ==== `TODAY`
  940. .Synopsis:
  941. [source, sql]
  942. --------------------------------------------------
  943. TODAY()
  944. --------------------------------------------------
  945. *Input*: _none_
  946. *Output*: date
  947. *Description*: This function offers the same functionality as <<sql-functions-current-date,CURRENT_DATE()>> function: returns
  948. the date when the current query reached the server. This method always returns the same value for its every occurrence
  949. within the same query.
  950. [source, sql]
  951. --------------------------------------------------
  952. include-tagged::{sql-specs}/docs/docs.csv-spec[todayFunction]
  953. --------------------------------------------------
  954. Typically, this function (as well as its twin <<sql-functions-current-timestamp,CURRENT_TIMESTAMP())>> function is used
  955. for relative date filtering:
  956. [source, sql]
  957. --------------------------------------------------
  958. include-tagged::{sql-specs}/docs/docs.csv-spec[filterToday]
  959. --------------------------------------------------
  960. [[sql-functions-datetime-week]]
  961. ==== `WEEK_OF_YEAR/WEEK`
  962. .Synopsis:
  963. [source, sql]
  964. --------------------------------------------------
  965. WEEK_OF_YEAR(datetime_exp) <1>
  966. --------------------------------------------------
  967. *Input*:
  968. <1> date/datetime expression. If `null`, the function returns `null`.
  969. *Output*: integer
  970. *Description*: Extract the week of the year from a date/datetime.
  971. [source, sql]
  972. --------------------------------------------------
  973. include-tagged::{sql-specs}/docs/docs.csv-spec[weekOfYear]
  974. --------------------------------------------------
  975. [[sql-functions-datetime-year]]
  976. ==== `YEAR`
  977. .Synopsis:
  978. [source, sql]
  979. --------------------------------------------------
  980. YEAR(datetime_exp) <1>
  981. --------------------------------------------------
  982. *Input*:
  983. <1> date/datetime expression. If `null`, the function returns `null`.
  984. *Output*: integer
  985. *Description*: Extract the year from a date/datetime.
  986. [source, sql]
  987. --------------------------------------------------
  988. include-tagged::{sql-specs}/docs/docs.csv-spec[year]
  989. --------------------------------------------------
  990. [[sql-functions-datetime-extract]]
  991. ==== `EXTRACT`
  992. .Synopsis:
  993. [source, sql]
  994. --------------------------------------------------
  995. EXTRACT(
  996. datetime_function <1>
  997. FROM datetime_exp) <2>
  998. --------------------------------------------------
  999. *Input*:
  1000. <1> date/time function name
  1001. <2> date/datetime expression
  1002. *Output*: integer
  1003. *Description*: Extract fields from a date/datetime by specifying the name of a <<sql-functions-datetime,datetime function>>.
  1004. The following
  1005. [source, sql]
  1006. --------------------------------------------------
  1007. include-tagged::{sql-specs}/docs/docs.csv-spec[extractDayOfYear]
  1008. --------------------------------------------------
  1009. is the equivalent to
  1010. [source, sql]
  1011. --------------------------------------------------
  1012. include-tagged::{sql-specs}/docs/docs.csv-spec[dayOfYear]
  1013. --------------------------------------------------