skip to content

In SQL, what does created_at AT TIME ZONE 'Europe/Berlin' do, and how does the answer depend on whether created_at has a time zone?

level: middleimportance: must knowfreq 50%

answer

  1. an instant or a wall-clock reading
  2. the output type flips
  3. the result prints in the session zone
  4. two steps for a plain UTC column
  5. POSIX signs point the other way

basics

~10 s

On PostgreSQL, AT TIME ZONE converts by input type: a timestamp with time zone becomes Berlin's wall-clock reading, and a plain timestamp is read as Berlin's wall clock and becomes an instant.

solid answer

~40 s

For timestamps, the operator converts between an instant and a wall-clock reading, so its direction depends on what you give it. On PostgreSQL, `timestamptz AT TIME ZONE 'Europe/Berlin'` takes an instant and returns a plain `timestamp` showing what Berlin's clocks read at that moment. `timestamp AT TIME ZONE 'Europe/Berlin'` takes a plain reading, assumes it was read in Berlin, and returns the instant as a `timestamptz`, which prints in the session's zone and surprises anyone expecting Berlin time. So converting a plain column that holds UTC takes two steps: `created_at AT TIME ZONE 'UTC' AT TIME ZONE 'Europe/Berlin'`. One step alone shifts the value the wrong way. Engines differ: SQL Server returns an offset-carrying type in both directions, Oracle reads a plain input in the session's zone, and MySQL uses `CONVERT_TZ()` for this conversion.

code

sql · 10 lines
sql
SET TimeZone = 'UTC';

SELECT TIMESTAMPTZ '2024-07-01 12:00+00' AT TIME ZONE 'Europe/Berlin';
-- 2024-07-01 14:00:00      (plain timestamp)

SELECT TIMESTAMP '2024-07-01 12:00' AT TIME ZONE 'Europe/Berlin';
-- 2024-07-01 10:00:00+00   (timestamp with time zone)

SELECT TIMESTAMP '2024-07-01 12:00' AT TIME ZONE 'UTC' AT TIME ZONE 'Europe/Berlin';
-- 2024-07-01 14:00:00      (a plain UTC value, converted in two steps)

go deeper

for a junior

Recall the difference between an instant and a wall-clock reading. Then show that AT TIME ZONE takes a zone and that its result type depends on the type you pass in.

for a middle

Trace both directions on a concrete value, and explain why a plain UTC column takes two steps. Be ready to say why a named zone beats a fixed offset, and what the sign means in a POSIX-style string.

for a senior

Check the column's real type and what its values mean before trusting any conversion. Look for single-step conversions, session-dependent casts and fixed offsets, since each gives plausible output that is an hour or more off.

for a principal

Set one convention for conversions across services, including which layer applies the zone and where zone names come from. Inconsistent conversion is expensive to find later, because each report looks correct when read alone.

## Two kinds of value Date-time columns hold one of two different things, and the operator only makes sense once you can tell them apart: - An **instant** is one point in time that everybody on Earth shares. On PostgreSQL, a `timestamp with time zone` (`timestamptz`) holds one: it is stored as UTC, and the zone it was entered in is not retained. - A **wall-clock reading** is what a clock displayed, such as 14:00 on 1 July. A plain `timestamp` holds one, and without a place attached it does not identify an instant. To get from one to the other you need a **time zone**, the rule that maps instants to readings for a place. `AT TIME ZONE` applies that rule, and the input's type tells it which direction to go. ## What the operator does on PostgreSQL With the session's `TimeZone` set to UTC: | Input | Expression | Result | Result type | |---|---|---|---| | instant | `TIMESTAMPTZ '2024-07-01 12:00+00' AT TIME ZONE 'Europe/Berlin'` | `2024-07-01 14:00:00` | plain `timestamp` | | reading | `TIMESTAMP '2024-07-01 12:00' AT TIME ZONE 'Europe/Berlin'` | `2024-07-01 10:00:00+00` | `timestamptz` | The second row is the surprising one. The value is the instant at which Berlin's clocks read 12:00, and a `timestamptz` always prints in the session's zone, so it shows 10:00 in UTC rather than anything resembling Berlin. Nothing is wrong with the result. Its display is simply governed by the session. ## A plain column that holds UTC Applications often write UTC into a plain `timestamp` column. Reaching Berlin time from there takes two steps: 1. `created_at AT TIME ZONE 'UTC'` says the reading was taken in UTC, which gives the instant. 2. `... AT TIME ZONE 'Europe/Berlin'` reads that instant on Berlin's clock, which gives a plain reading. ```sql -- created_at is a plain TIMESTAMP holding UTC SELECT created_at AT TIME ZONE 'UTC' AT TIME ZONE 'Europe/Berlin' AS berlin_time FROM orders; ``` Writing only `created_at AT TIME ZONE 'Europe/Berlin'` treats a UTC reading as a Berlin reading. A value of 12:00 comes out as the instant 10:00 UTC, so in summer it moves two hours backward when you wanted two hours forward. A cast such as `created_at::timestamptz AT TIME ZONE 'Europe/Berlin'` looks like a shortcut, but the cast reads the value in the session's zone, so it is right only when that session happens to be UTC. ## Naming the zone The zone argument can take several forms, and they do not behave alike: - **A named zone**, such as `'America/New_York'`, follows that place's clock changes and its history. This is the form to use for anything a person reads. - **An interval**, such as `INTERVAL '-05:00'`, is a fixed offset. The PostgreSQL manual notes it is useful only for zones with fixed offsets from UTC, so it is wrong for New York for much of the year. - **A POSIX-style string**, such as `'+05'` or `'UTC+5'`, has the **opposite sign** from an ISO 8601 offset. On PostgreSQL, `TIMESTAMPTZ '2024-07-01 12:00+00' AT TIME ZONE '+05'` gives 07:00, five hours **behind** UTC, while `AT TIME ZONE INTERVAL '+05:00'` gives 17:00. ## Readings a clock change makes awkward A plain reading cannot always name exactly one instant. PostgreSQL does not raise an error in either case, so a query will not warn you: - **A skipped reading.** On 10 March 2024, New York's clocks jump from 02:00 to 03:00, so 02:30 never happened. `TIMESTAMP '2024-03-10 02:30' AT TIME ZONE 'America/New_York'` still returns a value, 07:30 UTC. - **A repeated reading.** On 3 November 2024, 01:30 happens twice, at 05:30 UTC and at 06:30 UTC. `TIMESTAMP '2024-11-03 01:30' AT TIME ZONE 'America/New_York'` returns the later one, and the earlier instant cannot be reached from that reading. ## Engines differ | Engine | Form | Behaviour | |---|---|---| | PostgreSQL | `AT TIME ZONE`, plus `AT LOCAL` for the session's zone | the direction flips with the input type, as above | | SQL Server | `AT TIME ZONE` | returns an offset-carrying type in both directions | | Oracle | `AT TIME ZONE` | reads a plain input in the session's zone; `FROM_TZ` attaches a named one | | MySQL | `CONVERT_TZ(value, from, to)` | returns NULL for named zones until the zone tables are loaded | | SQLite | no such operator | its `utc` and `localtime` modifiers cover only the local zone |

  • Why does AT TIME ZONE '+05' on PostgreSQL show a time five hours behind UTC?
    A text offset like that is read as a POSIX-style zone, and POSIX signs have the opposite meaning from ISO 8601 offsets. So `'+05'` means five hours west of Greenwich, and 12:00 UTC shows as 07:00. Write `INTERVAL '+05:00'` for the ISO sense, or better, use a named zone.
  • Why is a fixed offset the wrong argument when the place has daylight saving?
    An offset never changes, while the place's clocks do. `INTERVAL '-05:00'` matches New York only in winter, so summer times come out an hour early. A named zone such as `'America/New_York'` applies the rule in force on each value's own date, including past rule changes.
  • What does PostgreSQL return when a plain reading falls in an hour that a clock change skipped?
    It returns a value rather than an error. `TIMESTAMP '2024-03-10 02:30' AT TIME ZONE 'America/New_York'` gives 07:30 UTC, although New York's clocks never showed 02:30 that day. So a bad local input passes silently, and any check for it has to happen in the application or a data test.

A flight departs at one instant, yet each airport's clock shows a different reading of it. Asking what Berlin's clock showed turns an instant into a reading. Being handed a reading from Berlin's clock lets you work out the instant. The operator does whichever of those its input calls for, and a reading with no city attached cannot be converted until you say which city it came from.

  • AT TIME ZONE only changes how a value is displayed, and its type stays the same on PostgreSQL.
  • On PostgreSQL, a plain timestamp AT TIME ZONE 'Europe/Berlin' prints Berlin's local time.
  • One AT TIME ZONE 'Europe/Berlin' is enough to convert a plain column holding UTC to Berlin time.
  • On PostgreSQL, the text zone '+05' in AT TIME ZONE means five hours east of UTC.
  • A fixed offset such as -05:00 matches America/New_York all year round.