to_datetime.asciidoc 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. [discrete]
  2. [[esql-to_datetime]]
  3. === `TO_DATETIME`
  4. Converts an input value to a date value.
  5. The input can be a single- or multi-valued field or an expression. The input
  6. type must be of a string or numeric type.
  7. A string will only be successfully converted if it's respecting the format
  8. `yyyy-MM-dd'T'HH:mm:ss.SSS'Z'` (to convert dates in other formats, use <<esql-date_parse>>). For example:
  9. [source.merge.styled,esql]
  10. ----
  11. include::{esql-specs}/date.csv-spec[tag=to_datetime-str]
  12. ----
  13. [%header.monospaced.styled,format=dsv,separator=|]
  14. |===
  15. include::{esql-specs}/date.csv-spec[tag=to_datetime-str-result]
  16. |===
  17. Note that in this example, the last value in the source multi-valued
  18. field has not been converted. The reason being that if the date format is not
  19. respected, the conversion will result in a *null* value. When this happens a
  20. _Warning_ header is added to the response. The header will provide information
  21. on the source of the failure:
  22. `"Line 1:112: evaluation of [TO_DATETIME(string)] failed, treating result as null. Only first 20 failures recorded."`
  23. A following header will contain the failure reason and the offending value:
  24. `"java.lang.IllegalArgumentException: failed to parse date field [1964-06-02 00:00:00] with format [yyyy-MM-dd'T'HH:mm:ss.SSS'Z']"`
  25. If the input parameter is of a numeric type, its value will be interpreted as
  26. milliseconds since the https://en.wikipedia.org/wiki/Unix_time[Unix epoch].
  27. For example:
  28. [source.merge.styled,esql]
  29. ----
  30. include::{esql-specs}/date.csv-spec[tag=to_datetime-int]
  31. ----
  32. [%header.monospaced.styled,format=dsv,separator=|]
  33. |===
  34. include::{esql-specs}/date.csv-spec[tag=to_datetime-int-result]
  35. |===
  36. Alias: TO_DT