Filter a date range with a half-open boundary
Write a date range that includes the start, excludes the next period, and leaves the filtered column unwrapped in a function.
What you are trying to do
A daily report needs every event on January 10, including times after midnight, and must avoid including January 11.
Work through it
- Read @StartInclusive and @EndExclusive as the boundaries of one day.
- Trace the greater than or equal start and less than end predicates.
- Notice that the filter compares the raw EventAt column rather than applying CONVERT or CAST to each row.
- Check all timestamps, including the final event before midnight.
Check your work
The result contains events 1 at midnight and 2 at 23:59:59.9999999 on January 10. It excludes event 3 at exactly midnight on January 11 and event 4 before January 10.
Practice SQL
Open in SQL FlowDECLARE @StartInclusive datetime2(7) = '2026-01-10T00:00:00';
DECLARE @EndExclusive datetime2(7) = '2026-01-11T00:00:00';
SELECT EventId, EventAt
FROM (VALUES
(1, CONVERT(datetime2(7), '2026-01-10T00:00:00')),
(2, CONVERT(datetime2(7), '2026-01-10T23:59:59.9999999')),
(3, CONVERT(datetime2(7), '2026-01-11T00:00:00')),
(4, CONVERT(datetime2(7), '2026-01-09T23:59:59.9999999'))
) AS Event(EventId, EventAt)
WHERE EventAt >= @StartInclusive AND EventAt < @EndExclusive
ORDER BY EventAt;
Knowledge check
Commands in this lesson
Next lesson Review a permission request at object scope