Do DST-safe day arithmetic on timestamps in SQL
Adding 24 hours to a zoned timestamp lands an hour off after a DST switch. Calendar-day addition must happen in local time.
The task
Convert the instant to local wall time, do the day addition there, and convert back. The local clock time survives the DST switch.
The snippet
Written for PostgreSQL. Versions for SQL Server are under Variations.
SELECT
(starts_at AT TIME ZONE 'America/New_York' + interval '1 day')
AT TIME ZONE 'America/New_York' AS next_day_same_local_time
FROM sessions;
它的運作方式
PostgreSQL adds interval '1 day' to a timestamptz as a calendar day in the session's TimeZone. Servers usually run sessions in UTC, where that is exactly 24 hours, so a New York session starting at 10:00 on 2026-03-07 lands at 11:00 local the next day, after the spring-forward switch. Converting to New York wall time, adding the day there and converting back keeps 10:00. SQL Server needs the same round trip with datetimeoffset, shown in the variant.
Variations
SQL Server (starts_at is datetimeoffset)
SQL ServerSELECT
CAST(DATEADD(DAY, 1, CAST(starts_at AT TIME ZONE 'Eastern Standard Time' AS datetime2)) AS datetime2)
AT TIME ZONE 'Eastern Standard Time' AS next_day_same_local_time
FROM sessions;
Questions that come up
Is SQL Server's DATEADD DST-safe on datetimeoffset?
No. DATEADD(DAY, 1, ...) on a datetimeoffset adds a day and keeps the original offset, so 2026-03-07 10:00 -05:00 becomes 2026-03-08 10:00 -05:00, which is 11:00 in New York after the switch. The variant drops to a wall-clock datetime2, adds the day, and lets AT TIME ZONE pick the offset that applies on the new date (-04:00).
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.