Convert text to ClickHouse Date or DateTime with toDate, toDateTime, parseDateTimeBestEffort, or CAST.
Use toDate()
for Date
, toDateTime()
for DateTime
, or parseDateTimeBestEffort()
when the input format varies. All accept a string literal or column.
Wrap the column in the desired function or use CAST(col AS Date)
. The result can be aliased for downstream joins or filtering.
Yes—run ALTER TABLE ... UPDATE
to overwrite the original column or insert into a new table with the converted column type.
Chain if
with like
patterns or default to parseDateTimeBestEffort()
, which recognises ISO-8601, RFC-2822, and common U.S./EU formats automatically.
To avoid implicit server-timezone shifts, pass a second argument: toDateTime('2024-05-10 08:30','UTC')
. For per-row zones, ensure the column already carries TZ info.
Store dates as Date
or DateTime
as early as possible, index partitions on dates, and prefer toDate( toDateTime(...))
instead of string ops in filters.
It is slightly slower than toDate/Time because it auto-detects formats. Use it only during ingestion or when input formats are unreliable.
Yes—use toDateTime64(epoch_seconds, 0, 'UTC')
or divide milliseconds by 1000 before calling toDateTime
.