Convert local wall time to UTC in SQL
Schedulers capture '2026-03-08 01:30 Eastern' but the database should store UTC. The conversion must respect DST gaps.
The task
Declare the wall time's zone, then convert: two AT TIME ZONE steps, DST-aware both times.
The snippet
Written for SQL Server. Versions for PostgreSQL are under Variations.
SELECT local_time
AT TIME ZONE 'Eastern Standard Time'
AT TIME ZONE 'UTC' AS utc_time
FROM schedule_items;
仕組み
The first AT TIME ZONE attaches the Eastern offset in force at that wall time to the naive datetime, producing a datetimeoffset; the second shifts it to +00:00, still as a datetimeoffset (CAST it to datetime2 to store a plain UTC value). On the spring-forward night a nonexistent local time like 02:30 is moved forward an hour (03:30 EDT, 07:30 UTC) on both SQL Server and PostgreSQL. On the fall-back night 01:30 happens twice and the engines disagree without warning: SQL Server takes the daylight offset (05:30 UTC), PostgreSQL the standard one (06:30 UTC).
Variations
PostgreSQL
PostgreSQLSELECT local_time
AT TIME ZONE 'America/New_York'
AT TIME ZONE 'UTC' AS utc_time
FROM schedule_items;
Questions that come up
Which zone names does each engine accept?
SQL Server wants Windows names ('Eastern Standard Time', 'W. Europe Standard Time'); PostgreSQL wants IANA names ('America/New_York', 'Europe/Berlin'). They are looked up in the OS or tzdata databases, so keep them configurable rather than hardcoded.
Keep reading
Run these with autocomplete that knows your tables
SQLly is a native SQL workbench for macOS, Windows and Linux. Paste a snippet into a live connection and you get schema-aware IntelliSense, a plan viewer and guards against unsafe edits.