Converts datetime values from one time zone to another using AT TIME ZONE or SWITCHOFFSET.
Global apps display local timestamps. Converting in-query keeps logic server-side and guarantees uniform calculations across clients.
SQL Server 2016+ offers AT TIME ZONE
for round-trip conversion. Versions 2008–2014 rely on SWITCHOFFSET
combined with DATEADD
.
AT TIME ZONE
pattern?SELECT datetime_value AT TIME ZONE 'SourceZone' AT TIME ZONE 'TargetZone';
attaches the source zone, then converts to the target.
order_date
to US PacificSELECT id, order_date AT TIME ZONE 'UTC' AT TIME ZONE 'Pacific Standard Time' AS pacific_timeFROM Orders;
Cast to datetime
or datetime2
: CAST(order_date AT TIME ZONE 'UTC' AT TIME ZONE 'Pacific Standard Time' AS datetime2)
.
Filter in UTC: compute @utc_start
/ @utc_end
, query, then convert results for presentation. This keeps index seeks fast.
Store timestamps in UTC, use named Windows zones (not numeric offsets), and add a persisted computed column if you must index local time.
Hard-coding offsets ignores daylight saving; always use zone names. Never apply AT TIME ZONE
to values already typed as datetimeoffset
.
Convert only selected columns, not predicates. Materialize local time in the presentation layer or a view, not in hot OLTP paths.
Join Orders with a Customer time-zone column and call AT TIME ZONE
per row, or pre-aggregate in UTC and convert in the reporting layer.
Yes. When you supply a Windows time-zone name, SQL Server applies the correct offset for each date, including DST transitions.
Yes, but AT TIME ZONE is unavailable. Use TODATETIMEOFFSET to mark the value as UTC, then SWITCHOFFSET to the desired offset.
Minimal in SELECT lists. Avoid using it in WHERE clauses on large tables; filter in UTC instead, then convert in the projection.