SQL Flow Field Guide

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

  1. Read @StartInclusive and @EndExclusive as the boundaries of one day.
  2. Trace the greater than or equal start and less than end predicates.
  3. Notice that the filter compares the raw EventAt column rather than applying CONVERT or CAST to each row.
  4. 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 Flow
DECLARE @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