SQL Flow Field Guide

Ask a temporal table for an earlier row version

Use FOR SYSTEM_TIME AS OF to read the version of a system-versioned row that was valid at a chosen time.

What you are trying to do

An incident review asks what title task 7 had on January 10. The first query simulates row validity with inline values. The second shows the SQL Server temporal syntax for a system-versioned table.

Work through it

  1. Run or visualize the first statement, which uses only inline synthetic version intervals.
  2. Check that the half-open interval includes its start and excludes its end.
  3. Inspect the FOR SYSTEM_TIME AS OF statement as read-only syntax study. It expects dbo.WorkItem to be system-versioned.
  4. Do not run the second statement against a live database. If you execute it, use an isolated database with the matching synthetic temporal table.

Check your work

The inline simulation returns exactly one row for task 7 with title Draft API and validity beginning 2026-01-01. The row ending at 2026-01-15 is excluded at January 10. The second statement parses as a read from dbo.WorkItem at one system time and changes no rows.

Practice SQL

Open in SQL Flow
DECLARE @AsOf datetime2(0) = '2026-01-10T12:00:00';
WITH Versions AS (
    SELECT TaskId, Title, ValidFrom, ValidTo
    FROM (VALUES
        (7, N'Draft API', CONVERT(datetime2(0), '2026-01-01T00:00:00'), CONVERT(datetime2(0), '2026-01-15T00:00:00')),
        (7, N'Review API', CONVERT(datetime2(0), '2026-01-15T00:00:00'), CONVERT(datetime2(0), '9999-12-31T23:59:59'))
    ) AS v(TaskId, Title, ValidFrom, ValidTo)
)
SELECT TaskId, Title, ValidFrom, ValidTo
FROM Versions
WHERE ValidFrom <= @AsOf AND @AsOf < ValidTo;
GO
-- SAFE READ-ONLY SYNTAX STUDY. Inspect only. Execute only in a disposable database with a matching system-versioned dbo.WorkItem table.
DECLARE @AsOf datetime2(0) = '2026-01-10T12:00:00';
SELECT WorkItemId, Title, Status
FROM dbo.WorkItem FOR SYSTEM_TIME AS OF @AsOf
WHERE WorkItemId = 7;

Knowledge check

Commands in this lesson

Next lesson Rank and compare rows without collapsing them