Skip to content

In development. Expect rough edges and changes. The source code is not public yet.

ClickHouse 4 min read How-to mediumAdept

5 ways to parse Dates and DateTimes in ClickHouse

ClickHouse offers five main routes for turning raw values into Date/DateTime types. `fromUnixTimestamp` and its 64Milli/64Micro variants convert epoch timestamps to `DateTime` or `DateTime64(3/6)`. `YYYYMMDDToDate` and `YYYYMMDDhhmmssToDateTime` handle packed numeric dates, and `parseDateTime` or `parseDateTimeInJodaSyntax` parse known format strings. For mixed or unknown formats, the `parseDateTimeBestEffort` family works, with OrNull and OrZero variants that avoid exceptions on invalid input. Plain `::DateTime` casts can also accept varied formats, including in queries over CSV files, once `cast_string_to_date_time_mode` is set to `best_effort` per query or per session.

Related posts

  • Querying DateTimes in ClickHouse

    ClickHouse mediumAdept

    ClickHouse's datetime functions cover rounding, filtering and duration calculations, demonstrated on about 3 million NYC taxi trips. toStartOfHour and toStartOfFifteenMinutes bucket timestamps, casting to ::Time groups or filters by time of day across all dates, and dateDiff computes trip durations to derive average speed, which falls from about 19 mph at 6am to about 11 mph by 8:30am. countIf with toDayOfWeek splits weekday and weekend trips, and the lag window function computes percentage change between windows. formatDateTime and bar make the output readable.

  • Working with Time Series Data in ClickHouse

    ClickHouse mediumAdept

    ClickHouse handles time-series workloads with Date, DateTime and DateTime64 types (up to nanosecond precision, with per-column timezones) plus SQL functions for bucketing by period. toStartOfHour and toStartOfInterval group by fixed or custom intervals, WITH FILL fills empty buckets, dateDiff builds offset rolling windows, and bar() gives quick inline visuals. Gauges can be filled forward with any() plus INTERPOLATE, and histogram() builds adaptive histograms. Examples run against a 1-billion-row Wikipedia pageviews table, where full scans complete in about 0.26 seconds.

  • ClickHouse and the MTA Data Challenge

    ClickHouse mediumAdept

    Loading and cleaning the ~95 million-row NYC MTA subway turnstile dataset (2014–2022) in ClickHouse, using schema inference over S3, a staging table, and an optimized schema with LowCardinality strings, a combined DateTime column, UInt32 counters, sorted line-name arrays and a (station, date_time) ordering key. Because entry/exit counters are cumulative odometer-style readings that reset, count down and overflow, window functions partitioned by turnstile (ca, unit, scp) compute per-period deltas and discard negative or implausible values above a per-hour threshold. The steps are reproducible with clickhouse-local, and the cleaned data is published in the ClickHouse SQL playground.

  • What's New in pg_clickhouse - JSONB Support, SQL value functions, Streaming, and more

    ClickHouse mediumAdept

    pg_clickhouse, the Postgres foreign data wrapper for querying ClickHouse, now pushes down far more operations to ClickHouse. JSONB accessors (->, ->>, jsonb_extract_path) are translated to ClickHouse sub-column syntax. CURRENT_DATE, CURRENT_TIMESTAMP and clock_timestamp() are translated to ClickHouse now()/now64()/nowInBlock64 calls that use the Postgres session time zone and timestamp precision. Array functions now map to their ClickHouse equivalents. Streaming of HTTP driver results in roughly 50MB batches kept container memory under about 100MiB during a full NYC taxi table scan, versus about 600MiB without it.

  • An Introduction to Data Formats in ClickHouse

    ClickHouse mediumAdept

    ClickHouse can import and export a wide range of formats through the FORMAT clause and related settings. Covered here: CSV and TSV with custom delimiters, CustomSeparated for broken CSVs, JSONEachRow for NDJSON, Regexp and Template for custom text, Native and RowBinary for efficient transfer between servers, Parquet and other Apache formats, and SQLInsert/MySQLDump for SQL dumps. Two practical tricks: the Null format benchmarks queries without printing output, and Pretty* variants control how CLI results are displayed. clickhouse-local with the file() function lets you inspect local files before loading them.

The quest for software engineering knowledge.

Look:
Layout: