to_datetime.asciidoc 1.6 KB

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