DateTime

Allows to store an instant in time, that can be expressed as a calendar date and a time of a day. DateTime allows to take into account time zones for stored values.

Syntax:

DateTime([timezone])

Supported range of values: [1970-01-01 00:00:00, 2105-12-31 23:59:59].

Resolution: 1 second.

SELECT toDateTime(0) FORMAT TSV

0000-00-00 00:00:00

Usage Remarks

A moment of time is stored as Unix timestamp, independently of time zones and daylight savings. Additionally DateTime can store time zone, that affects how DateTime values are displayed in text format and how input strings are parsed for storage. The tzdata package, containing IANA Time Zone Database, should be installed in the system. Use the timedatectl list-timezones command to list timezones known by a local system.

You can explicitly set a time zone for DateTime-type columns when creating a table. If the time zone isn't set, ClickHouse uses the value of the timezone parameter in the server settings or the operating system settings at the moment of the ClickHouse server start.

The clickhouse-client applies the server time zone by default if a time zone isn't explicitly set when initializing the data type. To use the client time zone, run clickhouse-client with the --use_client_time_zone parameter.

ClickHouse outputs values in YYYY-MM-DD hh:mm:ss text format by default. You can change the format with the formatDateTime function.

When inserting data into ClickHouse, you can use different formats of date and time strings, depending on the value of the date_time_input_format setting.

Examples

1. Creating a table with a DateTime-type column and inserting data into it:

CREATE TABLE dt
(
    `timestamp` DateTime('Europe/Moscow'), 
    `event_id` UInt8
)
ENGINE = TinyLog
INSERT INTO dt Values (1546300800, 1), ('2019-01-01 00:00:00', 2)
SELECT * FROM dt
┌───────────timestamp─┬─event_id─┐
│ 2019-01-01 03:00:00 │        1 │
│ 2019-01-01 00:00:00 │        2 │
└─────────────────────┴──────────┘

Unix timestamp 1546300800 represents the '2019-01-01 00:00:00' date and time in Europe/London (UTC+0) time zone, but the timestamp column stores values in the Europe/Moscow (UTC+3) timezone, so the value inserted as Unix timestamp is formatted as 2019-01-01 03:00:00.

SELECT * FROM dt WHERE timestamp = toDateTime('2019-01-01 00:00:00', 'Europe/Moscow')
┌───────────timestamp─┬─event_id─┐
│ 2019-01-01 00:00:00 │        2 │
└─────────────────────┴──────────┘

2. Getting a time zone for a DateTime-type value:

SELECT toDateTime(now(), 'Europe/Moscow') AS column, toTypeName(column) AS x
┌──────────────column─┬─x─────────────────────────┐
│ 2019-10-16 04:12:04 │ DateTime('Europe/Moscow') │
└─────────────────────┴───────────────────────────┘

See Also