> ## Documentation Index
> Fetch the complete documentation index at: https://clickhouse.com/docs/llms.txt
> Use this file to discover all available pages before exploring further.

# How to convert timestamps using a time zone stored in another column

> toTimeZone fails with ILLEGAL_COLUMN when the time zone comes from a column. Learn why the time zone must be constant and how to get the local time of each row with toString or formatDateTime.

Functions that return a `DateTime`, such as `toTimeZone`, accept only a constant time zone, so they fail when the time zone comes from a column. To get the local time of each row, convert the value to a string with [`toString`](https://clickhouse.com/docs/reference/functions/regular-functions/type-conversion-functions#toString) or [`formatDateTime`](https://clickhouse.com/docs/reference/functions/regular-functions/date-time-functions#formatDateTime). Both accept a time zone that differs from row to row.

## The error

```sql theme={null}
CREATE TABLE meetings
(
    id UInt32,
    starts_at DateTime('UTC'),
    time_zone String
)
ENGINE = MergeTree
ORDER BY id;

INSERT INTO meetings VALUES
    (1, '2024-11-15 23:11:01', 'Europe/Amsterdam'),
    (2, '2024-11-15 23:11:01', 'Asia/Tokyo'),
    (3, '2024-11-15 23:11:01', 'America/New_York');
```

```sql theme={null}
SELECT
    id,
    toTimeZone(starts_at, time_zone) AS local_time
FROM meetings
ORDER BY id;
```

```text theme={null}
Code: 44. DB::Exception: Illegal column time_zone of time zone argument of function, must be a constant string: In scope SELECT id, toTimeZone(starts_at, time_zone) AS local_time FROM meetings ORDER BY id ASC. (ILLEGAL_COLUMN)
```

## Why the time zone must be constant

In ClickHouse, the time zone is not stored with each value. It is a property of the data type: `DateTime('Asia/Tokyo')` and `DateTime('UTC')` are different types that store the same number of seconds since the Unix epoch and differ only in how values are converted to and from text. All values of a column have one type, so the result of [`toTimeZone`](https://clickhouse.com/docs/reference/functions/regular-functions/date-time-functions#toTimezone) can have only one time zone, and it has to be known before the query runs.

## Get the local time as a string

A `String` result has no time zone attached, so `toString` and `formatDateTime` can use a different time zone for every row:

```sql theme={null}
SELECT
    id,
    time_zone,
    starts_at,
    toString(starts_at, time_zone) AS local_time,
    formatDateTime(starts_at, '%d %b %Y, %H:%i', time_zone) AS formatted
FROM meetings
ORDER BY id;
```

```text theme={null}
   ┌─id─┬─time_zone────────┬───────────starts_at─┬─local_time──────────┬─formatted──────────┐
1. │  1 │ Europe/Amsterdam │ 2024-11-15 23:11:01 │ 2024-11-16 00:11:01 │ 16 Nov 2024, 00:11 │
2. │  2 │ Asia/Tokyo       │ 2024-11-15 23:11:01 │ 2024-11-16 08:11:01 │ 16 Nov 2024, 08:11 │
3. │  3 │ America/New_York │ 2024-11-15 23:11:01 │ 2024-11-15 18:11:01 │ 15 Nov 2024, 18:11 │
   └────┴──────────────────┴─────────────────────┴─────────────────────┴────────────────────┘
```

The column must contain time zone names that are listed in `system.time_zones`. A row with an unknown name makes the query fail.

## Get the hour or the date of the local time

If you need parts of the local time, such as the hour or the calendar date, parse the string back into a `DateTime64` with a fixed time zone and apply the usual functions to it:

```sql theme={null}
SELECT
    id,
    time_zone,
    toDateTime64(toString(starts_at, time_zone), 0, 'UTC') AS wall_clock,
    toHour(wall_clock) AS local_hour,
    toDate32(wall_clock) AS local_date
FROM meetings
ORDER BY id;
```

```text theme={null}
   ┌─id─┬─time_zone────────┬──────────wall_clock─┬─local_hour─┬─local_date─┐
1. │  1 │ Europe/Amsterdam │ 2024-11-16 00:11:01 │          0 │ 2024-11-16 │
2. │  2 │ Asia/Tokyo       │ 2024-11-16 08:11:01 │          8 │ 2024-11-16 │
3. │  3 │ America/New_York │ 2024-11-15 18:11:01 │         18 │ 2024-11-15 │
   └────┴──────────────────┴─────────────────────┴────────────┴────────────┘
```

> [!WARNING]
>
> `wall_clock` shows the local time of each row, but it is no longer the same point in time as `starts_at`: the three rows started at the same moment and now hold three different values. Use it to extract parts of the local time or to group by them. Do not compare it with real timestamps or store it as the time of the event.

Pass the time zone to `toDateTime64` explicitly, as `'UTC'` in this example. Without it, the string is interpreted in the time zone of the server or of the session, and the result depends on where the query runs.

The query uses `toDateTime64` and `toDate32` instead of `toDateTime` and `toDate`, because the local time of a `DateTime` value can lie outside the range of `DateTime` and `Date`, which both start on 1970-01-01. For example, `1970-01-01 00:00:00` UTC is `1969-12-31 19:00:00` in `America/New_York`. `toDateTime` and `toDate` return a wrong value for such a local time without an error, while [`DateTime64`](https://clickhouse.com/docs/reference/data-types/datetime64) and [`Date32`](https://clickhouse.com/docs/reference/data-types/date32) cover it.

## Do not enable allow\_nonconst\_timezone\_arguments

The [`allow_nonconst_timezone_arguments`](https://clickhouse.com/docs/reference/settings/session-settings/allow#allow_nonconst_timezone_arguments) setting makes the error disappear, but it does not convert the values. It exists only for compatibility. With the setting enabled, the non-constant argument is ignored, and the result keeps the time zone of the input column:

```sql theme={null}
SELECT
    id,
    toTimeZone(starts_at, time_zone) AS local_time,
    toTypeName(local_time) AS type
FROM meetings
ORDER BY id
SETTINGS allow_nonconst_timezone_arguments = 1;
```

```text theme={null}
   ┌─id─┬──────────local_time─┬─type────────────┐
1. │  1 │ 2024-11-15 23:11:01 │ DateTime('UTC') │
2. │  2 │ 2024-11-15 23:11:01 │ DateTime('UTC') │
3. │  3 │ 2024-11-15 23:11:01 │ DateTime('UTC') │
   └────┴─────────────────────┴─────────────────┘
```

## Related pages

* [`DateTime` data type](https://clickhouse.com/docs/reference/data-types/datetime)
* [`toTimeZone` function](https://clickhouse.com/docs/reference/functions/regular-functions/date-time-functions#toTimezone)
* [`toString` function](https://clickhouse.com/docs/reference/functions/regular-functions/type-conversion-functions#toString)
* [`formatDateTime` function](https://clickhouse.com/docs/reference/functions/regular-functions/date-time-functions#formatDateTime)
