How do you query SQL Server records with dates greater than a specific date? | A1Lab@HumairaM to SQLEnglish • 2 days ago

How do you query SQL Server records with dates greater than a specific date?

message-square
0

To query SQL Server records with dates strictly greater than a specific date, use the > operator with an unambiguous ISO date literal (YYYY-MM-DD or YYYY-MM-DDTHH:MM:SS) in the WHERE clause. When querying DATETIME, DATETIME2, or SMALLDATETIME columns, remember that '2026-11-30' represents midnight (00:00:00.000) at the start of that day—so > '2026-11-30' returns records starting after midnight on November 30 (including later times on November 30 and all of December 1 onward), whereas >= '2026-12-01' returns records starting on December 1.


1. Basic > Comparison on a DATE or DATETIME Column

Keep the column unencumbered on the left side of > so SQL Server can perform an index seek on large tables.

SELECT
    order_id,
    customer_id,
    created_at,
    total_amount
FROM dbo.orders
WHERE created_at > '2026-11-30'
ORDER BY created_at ASC;
  • If created_at is DATETIME or DATETIME2, '2026-11-30' implicitly converts to 2026-11-30 00:00:00.000, matching rows with timestamps strictly later than midnight on November 30, 2026.
  • If created_at is DATE, '2026-11-30' matches rows with dates strictly after 2026-11-30 (2026-12-01 and later).

2. Filtering Strictly After a Full Calendar Day (> End of Day)

If you want rows strictly after all timestamps on 2026-11-30 (meaning starting at 2026-12-01 00:00:00.000), use the next day’s midnight boundary rather than guessing fractional seconds like 23:59:59.997:

SELECT
    order_id,
    created_at,
    total_amount
FROM dbo.orders
WHERE created_at >= '2026-12-01T00:00:00';
  • Using >= '2026-12-01T00:00:00' (or > '2026-11-30T23:59:59.9999999' on DATETIME2) avoids precision truncation issues across DATETIME (3.33ms rounding) and DATETIME2 (100ns precision).

3. Comparing Against Dynamic Dates (GETDATE(), SYSUTCDATETIME(), DATEADD())

Use SQL Server date/time functions when comparing against the current date or a relative offset:

-- Records from the last 7 days (using local server time)
SELECT
    event_id,
    event_timestamp,
    event_type
FROM dbo.activity_log
WHERE event_timestamp > DATEADD(DAY, -7, GETDATE());

-- Records strictly after today at 00:00:00 (UTC)
SELECT
    event_id,
    event_timestamp,
    event_type
FROM dbo.activity_log
WHERE event_timestamp > CAST(CAST(SYSUTCDATETIME() AS DATE) AS DATETIME2);

4. Index-Friendly Practices (Avoid Non-SARGable Predicates)

Avoid wrapping the column in functions on the left side of > when performance matters:

-- Avoid (prevents clean index seek on created_at):
SELECT order_id, created_at
FROM dbo.orders
WHERE CAST(created_at AS DATE) > '2026-11-30';

-- Prefer (index-friendly):
SELECT order_id, created_at
FROM dbo.orders
WHERE created_at > '2026-11-30T00:00:00';

Key Rules to Remember

  1. Use ISO format (YYYY-MM-DD or YYYY-MM-DDTHH:MM:SS) to prevent regional MM/DD vs. DD/MM conversion issues.
  2. Account for 00:00:00 midnight default when comparing date strings against DATETIME or DATETIME2 columns.
  3. Keep the column bare on the left side of > so SQL Server indexes remain effective.