Interview question
Query a local business date with a SARGable UTC range
Converts local-day boundaries to UTC once, preserving correct daylight-saving behavior and index use on stored UTC timestamps.
TL;DR
Converts local-day boundaries to UTC once, preserving correct daylight-saving behavior and index use on stored UTC timestamps.
Time-zone boundaries, daylight-saving changes, half-open ranges, SARGability, and timestamp modeling.
Practice the problem like a real interview: restate, reason, implement, and test.
Table: Orders(OrderId, CreatedAtUtc, Status), where CreatedAtUtc is datetimeoffset in UTC. Parameters are @LocalDate and a validated Windows time-zone name such as Central European Standard Time. Return orders whose local timestamp falls on that date.
A daylight-saving transition day may contain 23 or 25 hours. Converting midnight at the start and next midnight independently produces the correct UTC interval instead of assuming every local day is 24 hours.
CreatedAtUtc consistently stored as UTC.I create local midnight for the requested date and the next date, attach the requested time zone to each boundary, then convert both boundaries to UTC. The final predicate compares the unchanged indexed column against constants. Computing both boundaries independently handles daylight-saving transitions correctly.
DECLARE @LocalStart datetime2 = CONVERT(datetime2, @LocalDate);
DECLARE @LocalEnd datetime2 = DATEADD(day, 1, @LocalStart);
DECLARE @UtcStart datetimeoffset =
(@LocalStart AT TIME ZONE @WindowsTimeZone) AT TIME ZONE 'UTC';
DECLARE @UtcEnd datetimeoffset =
(@LocalEnd AT TIME ZONE @WindowsTimeZone) AT TIME ZONE 'UTC';
SELECT o.OrderId, o.CreatedAtUtc, o.Status
FROM dbo.Orders AS o
WHERE o.CreatedAtUtc >= @UtcStart
AND o.CreatedAtUtc < @UtcEnd
ORDER BY o.CreatedAtUtc, o.OrderId;
The final range can seek on an index beginning with CreatedAtUtc. Converting every row with AT TIME ZONE or CONVERT(date, ...) would add CPU work and often prevent a simple seek. Boundary conversion is constant work per request.
CreatedAtUtc.Next in Query Practice: Atomic Inventory Reserve