to_datetime.asciidoc 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  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,esql]
  9. ----
  10. include::{esql-specs}/date.csv-spec[tag=to_datetime-str]
  11. ----
  12. returns:
  13. [%header,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,esql]
  29. ----
  30. include::{esql-specs}/date.csv-spec[tag=to_datetime-int]
  31. ----
  32. returns:
  33. [%header,format=dsv,separator=|]
  34. |===
  35. include::{esql-specs}/date.csv-spec[tag=to_datetime-int-result]
  36. |===
  37. Alias: TO_DT