to_datetime.asciidoc 1.7 KB

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