> ## 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.

# Why does a parameterized view with a DateTime parameter return different rows than the same query on the table?

> A DateTime parameter of a parameterized view is read in the server time zone, not in the time zone of the column it is compared with. Learn how to make both agree.

A parameter declared as `{name:DateTime}` has no time zone of its own, so its value is read in the server time zone (or in the [`session_timezone`](https://clickhouse.com/docs/reference/settings/session-settings/other#session_timezone), if it is set). A string literal compared with a `DateTime('Europe/Rome')` column is read in the time zone of that column. If the two time zones differ, the parameterized view and the direct query filter by different points in time. Declare the parameter with the time zone of the column, `{name:DateTime('Europe/Rome')}`, to make them agree.

## Reproduce the difference

The outputs below are from a server whose time zone is UTC. Check yours with `SELECT serverTimeZone()`.

```sql theme={null}
CREATE TABLE events
(
    id UInt64,
    timestamp DateTime('Europe/Rome')
)
ENGINE = MergeTree
ORDER BY id;

INSERT INTO events VALUES
    (1, '2024-01-01 12:30:00'),
    (2, '2024-01-01 12:31:00'),
    (3, '2024-01-01 12:31:30'),
    (4, '2024-01-01 15:15:32');
```

A direct query with a string literal returns three rows:

```sql theme={null}
SELECT *
FROM events
WHERE timestamp <= '2024-01-01 15:00:00'
ORDER BY id;
```

```text theme={null}
   ┌─id─┬───────────timestamp─┐
1. │  1 │ 2024-01-01 12:30:00 │
2. │  2 │ 2024-01-01 12:31:00 │
3. │  3 │ 2024-01-01 12:31:30 │
   └────┴─────────────────────┘
```

A parameterized view with the same condition returns four rows:

```sql theme={null}
CREATE VIEW events_until AS
SELECT *
FROM events
WHERE timestamp <= {end_date:DateTime};

SELECT *
FROM events_until(end_date = '2024-01-01 15:00:00')
ORDER BY id;
```

```text theme={null}
   ┌─id─┬───────────timestamp─┐
1. │  1 │ 2024-01-01 12:30:00 │
2. │  2 │ 2024-01-01 12:31:00 │
3. │  3 │ 2024-01-01 12:31:30 │
4. │  4 │ 2024-01-01 15:15:32 │
   └────┴─────────────────────┘
```

## Why this happens

A `DateTime` value is stored as a point in time, and the time zone only defines how text is converted to and from it.

* In the direct query, the string `'2024-01-01 15:00:00'` is converted to the type of the column it is compared with, `DateTime('Europe/Rome')`. It means 15:00 in Rome.
* In the view, the value is converted to the type of the parameter first. `DateTime` without a time zone uses the server time zone, so on a UTC server it means 15:00 UTC, which is 16:00 in Rome. The row with `15:15:32` Rome time is before that moment and passes the filter.

```sql theme={null}
SELECT
    toDateTime('2024-01-01 15:00:00', 'UTC') AS parameter_value,
    toTimeZone(parameter_value, 'Europe/Rome') AS same_instant_in_rome;
```

```text theme={null}
   ┌─────parameter_value─┬─same_instant_in_rome─┐
1. │ 2024-01-01 15:00:00 │  2024-01-01 16:00:00 │
   └─────────────────────┴──────────────────────┘
```

This is not specific to views. A [query parameter](https://clickhouse.com/docs/reference/syntax.md#defining-and-using-query-parameters) of type `DateTime` in an ordinary query behaves the same way.

## Fix: give the parameter the time zone of the column

```sql theme={null}
CREATE VIEW events_until_rome AS
SELECT *
FROM events
WHERE timestamp <= {end_date:DateTime('Europe/Rome')};

SELECT *
FROM events_until_rome(end_date = '2024-01-01 15:00:00')
ORDER BY id;
```

```text theme={null}
   ┌─id─┬───────────timestamp─┐
1. │  1 │ 2024-01-01 12:30:00 │
2. │  2 │ 2024-01-01 12:31:00 │
3. │  3 │ 2024-01-01 12:31:30 │
   └────┴─────────────────────┘
```

The result no longer depends on the time zone of the server or of the session.

## Alternatives

Declare the parameter as `String`. The value is then converted in the comparison, in the time zone of the column, exactly like a literal in the direct query:

```sql theme={null}
CREATE VIEW events_until_string AS
SELECT *
FROM events
WHERE timestamp <= {end_date:String};

SELECT *
FROM events_until_string(end_date = '2024-01-01 15:00:00')
ORDER BY id;
```

```text theme={null}
   ┌─id─┬───────────timestamp─┐
1. │  1 │ 2024-01-01 12:30:00 │
2. │  2 │ 2024-01-01 12:31:00 │
3. │  3 │ 2024-01-01 12:31:30 │
   └────┴─────────────────────┘
```

Or keep the view as it is and set `session_timezone` for the query or the session. The setting changes the implicit time zone of every `DateTime` value without an explicit time zone in the query, not only of the parameter:

```sql theme={null}
SELECT *
FROM events_until(end_date = '2024-01-01 15:00:00')
ORDER BY id
SETTINGS session_timezone = 'Europe/Rome';
```

```text theme={null}
   ┌─id─┬───────────timestamp─┐
1. │  1 │ 2024-01-01 12:30:00 │
2. │  2 │ 2024-01-01 12:31:00 │
3. │  3 │ 2024-01-01 12:31:30 │
   └────┴─────────────────────┘
```

## Related pages

* [Parameterized views](https://clickhouse.com/docs/reference/statements/create/view.md#parameterized-view)
* [How to use parameterized views](https://clickhouse.com/docs/resources/support-center/knowledge-base/materialized-views/how-to-use-parametrised-views.md)
* [`DateTime` data type](https://clickhouse.com/docs/reference/data-types/datetime)
* [`session_timezone` setting](https://clickhouse.com/docs/reference/settings/session-settings/other#session_timezone)
