> ## 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 inserting an ISO 8601 date and time string into a Date column fail?

> The date_time_input_format setting applies to DateTime and DateTime64 only. A Date column accepts YYYY-MM-DD, so a value such as 2018-06-08T01:02:03.000Z must be converted during the insert.

The [`date_time_input_format`](https://clickhouse.com/docs/reference/settings/formats/date-time#date_time_input_format) setting selects the text parser for `DateTime` and `DateTime64` values only. A `Date` or `Date32` column has its own parser, which reads a date string only in the plain `YYYY-MM-DD` form, so a date with time such as `2018-06-08T01:02:03.000Z` is rejected whatever the value of the setting. To load such values, read them as `DateTime` and convert them with `toDate`, or store the full timestamp.

## Reproducing the error

```sql theme={null}
CREATE TABLE events
(
    id UInt32,
    event_date Date
)
ENGINE = MergeTree
ORDER BY id;

INSERT INTO events FORMAT JSONEachRow {"id": 1, "event_date": "2018-06-08T01:02:03.000Z"}
```

```text theme={null}
Code: 27. DB::Exception: Cannot parse input: expected '"' before: 'T01:02:03.000Z"}': (while reading the value of key event_date): (at row 1)
: While executing ParallelParsingBlockInputFormat. (CANNOT_PARSE_INPUT_ASSERTION_FAILED)
```

The parser reads `2018-06-08`, expects the value to end there, and finds `T01:02:03.000Z` instead. With `INSERT ... VALUES` the message is different, but the cause is the same:

```sql theme={null}
INSERT INTO events VALUES (1, '2018-06-08T01:02:03.000Z');
```

```text theme={null}
Code: 6. DB::Exception: Cannot parse string '2018-06-08T01:02:03.000Z' as Date: syntax error at position 23 (parsed just '2018-06-08T01:02:03.000'): while executing 'FUNCTION if(isNull(-dummy-0) : 3, defaultValueOfTypeName('Date') :: 2, _CAST(-dummy-0, 'Date') :: 4) -> if(isNull(-dummy-0), defaultValueOfTypeName('Date'), _CAST(-dummy-0, 'Date')) Date : 1': While executing ValuesBlockInputFormat. (CANNOT_PARSE_TEXT)
```

Both inserts ran with `date_time_input_format = 'best_effort'`, which is the default since ClickHouse 26.5 (`basic` before). The `best_effort` parser understands ISO 8601, but it is not used for `Date` and `Date32` columns.

## Convert the value during the insert

Describe the incoming data with the [`input`](https://clickhouse.com/docs/reference/functions/table-functions/input) table function. Declare the field as `DateTime`, so that the `best_effort` parser reads it, and convert it with `toDate`. With this `events.jsonl` file:

```json theme={null}
{"id": 1, "event_date": "2018-06-08T01:02:03.000Z"}
{"id": 2, "event_date": "2018-06-09T23:30:00+02:00"}
```

```bash theme={null}
clickhouse-client --query "
    INSERT INTO events
    SELECT
        id,
        toDate(event_date)
    FROM input('id UInt32, event_date DateTime(\'UTC\')')
    FORMAT JSONEachRow" < events.jsonl
```

```sql theme={null}
SELECT * FROM events ORDER BY id;
```

```text theme={null}
   ┌─id─┬─event_date─┐
1. │  1 │ 2018-06-08 │
2. │  2 │ 2018-06-09 │
   └────┴────────────┘
```

If you cannot change the query that the client sends, point the client at a landing table with the [`Null`](https://clickhouse.com/docs/reference/engines/table-engines/special/null) engine and let a materialized view do the same conversion:

```sql theme={null}
CREATE TABLE events_landing
(
    id UInt32,
    event_date DateTime('UTC')
)
ENGINE = Null;

CREATE MATERIALIZED VIEW events_landing_mv TO events AS
SELECT
    id,
    toDate(event_date) AS event_date
FROM events_landing;
```

An `INSERT INTO events_landing FORMAT JSONEachRow` with the same two rows inserts the same two rows into `events`.

## Or keep the time

If the source sends a timestamp, you can store it as one and derive the date from it:

```sql theme={null}
CREATE TABLE events_with_time
(
    id UInt32,
    event_time DateTime64(3, 'UTC'),
    event_date Date MATERIALIZED toDate(event_time)
)
ENGINE = MergeTree
ORDER BY id;

INSERT INTO events_with_time FORMAT JSONEachRow {"id": 1, "event_time": "2018-06-08T01:02:03.000Z"} {"id": 2, "event_time": "2018-06-09T23:30:00+02:00"}

SELECT
    id,
    event_time,
    event_date
FROM events_with_time
ORDER BY id;
```

```text theme={null}
   ┌─id─┬──────────────event_time─┬─event_date─┐
1. │  1 │ 2018-06-08 01:02:03.000 │ 2018-06-08 │
2. │  2 │ 2018-06-09 21:30:00.000 │ 2018-06-09 │
   └────┴─────────────────────────┴────────────┘
```

## Mind the time zone

A string with `Z` or a UTC offset is a point in time, and its calendar date depends on the time zone in which you look at it. `toDate` uses the time zone of the `DateTime` value, which is the server or session time zone unless you set one. The examples above pin it to `UTC`. To keep the date exactly as it is written in the string, take its first 10 characters instead:

```sql theme={null}
SELECT
    toDate(parseDateTimeBestEffort('2018-06-08T01:02:03.000Z', 'UTC')) AS utc_date,
    toDate(parseDateTimeBestEffort('2018-06-08T01:02:03.000Z', 'America/New_York')) AS new_york_date,
    toDate(substring('2018-06-08T01:02:03.000Z', 1, 10)) AS date_as_written;
```

```text theme={null}
   ┌───utc_date─┬─new_york_date─┬─date_as_written─┐
1. │ 2018-06-08 │    2018-06-07 │      2018-06-08 │
   └────────────┴───────────────┴─────────────────┘
```

> [!NOTE]
>
> `INSERT ... VALUES` accepts a date with time that has no time zone suffix, such as `'2018-06-08 01:02:03'` or `'2018-06-08T01:02:03.000'`, for a `Date` column and drops the time. This is specific to the `Values` format, which evaluates a value it cannot parse directly as an SQL expression. The same value is rejected in `JSONEachRow`, `CSV` and `TSV`, so do not rely on it.

## Related pages

* [`date_time_input_format` setting](https://clickhouse.com/docs/reference/settings/formats/date-time#date_time_input_format)
* [`Date` data type](https://clickhouse.com/docs/reference/data-types/date)
* [`DateTime64` data type](https://clickhouse.com/docs/reference/data-types/datetime64)
* [`parseDateTimeBestEffort` function](https://clickhouse.com/docs/reference/functions/regular-functions/type-conversion-functions#parseDateTimeBestEffort)
