ClickHouse formats Date or DateTime values with the formatDateTime() function, letting you output custom strings using strftime-style tokens and optional timezone.
Readable dates simplify reports and API responses. formatDateTime() converts Date or DateTime columns into any string pattern, avoiding extra parsing in application code.
Use formatDateTime(datetime_expression, 'pattern' [, 'timezone'])
. The pattern follows strftime tokens such as %Y
(year) or %d
(day). Add an optional IANA timezone like 'UTC'
or 'America/New_York'
.
SELECT
formatDateTime(order_date, '%Y-%m-%d') AS order_day,
total_amount
FROM Orders
ORDER BY order_day;
The query returns 2024-06-01
-style strings, perfect for grouping or CSV exports.
SELECT
formatDateTime(order_date, '%Y-%m-%d %H:%M:%S', 'America/Los_Angeles') AS local_time
FROM Orders
WHERE id = 42;
The third argument converts the stored UTC timestamp to Pacific Time before formatting.
Common tokens include %Y
year, %m
month, %d
day, %H
hour, %M
minute, %S
second, %F
ISO date, and %R
24-hour time. Combine them for flexible outputs.
Store timestamps in UTC, apply timezone only when displaying. Keep patterns consistent across dashboards. Cache formatted results for heavy reports, or create a MATERIALIZED VIEW with pre-formatted columns.
Yes. Use formatDateTime(nullable_col, '%F')
; nulls remain null.
No. formatDateTime is applied at read time. Use a MATERIALIZED VIEW if you need the string for filtering.
Include %f
in the pattern: formatDateTime(ts, '%Y-%m-%d %H:%M:%S.%f')
.