Sub topic 8
Date and Timestamp Ranges: Avoiding the End-of-Day Trap
Assume you know basic SELECT statements, comparison operators, and WHERE clauses. This chapter focuses on filtering dates and timestamps safely, especially when a range includes a complete calendar period.
Dates and timestamps are different values
A date contains a calendar day, such as 2026-03-31. A timestamp contains a day and a time, such as 2026-03-31 23:45:12.
A date literal used as a timestamp usually means midnight at the start of that date:
TIMESTAMP '2026-04-01 00:00:00'This detail matters when you filter a timestamp column.
Suppose orders.created_at stores timestamps and you want every order created in March 2026. This query looks reasonable:
SELECT id, created_at
FROM orders
WHERE created_at BETWEEN '2026-03-01' AND '2026-03-31';However, BETWEEN includes both endpoints. The upper endpoint means midnight at the start of March 31:
created_at >= 2026-03-01 00:00:00
AND created_at <= 2026-03-31 00:00:00The query includes only the first instant of March 31. It excludes an order created at 2026-03-31 14:30:00.
Use a half-open range for timestamps
A half-open range includes its lower endpoint and excludes its upper endpoint. Write the March filter like this:
SELECT id, created_at
FROM orders
WHERE created_at >= TIMESTAMP '2026-03-01 00:00:00'
AND created_at < TIMESTAMP '2026-04-01 00:00:00';This includes every timestamp from the start of March through the last possible instant before April:
2026-03-01 00:00:00 <= created_at < 2026-04-01 00:00:00The same pattern works for days, months, quarters, and other periods.
flowchart TD
A["Start: 2026-03-01 00:00:00"] --> B["Include all timestamps in March"]
B --> C["End boundary: 2026-04-01 00:00:00"]
C --> D["Exclude the end boundary"]Why not use the last possible time of the day?
You might try this:
WHERE created_at >= '2026-03-01 00:00:00'
AND created_at <= '2026-03-31 23:59:59'This can still miss rows if the database stores fractional seconds, such as 23:59:59.500000. It also depends on choosing the correct timestamp precision. The half-open form avoids both problems.
> callout note > For timestamp ranges, prefer >= start AND < next_period_start. Do not calculate an artificial “last moment” for the period.
Date columns and timestamp columns
For a column whose data type is DATE, an inclusive range is usually clear:
SELECT id, holiday_date
FROM holidays
WHERE holiday_date BETWEEN DATE '2026-03-01' AND DATE '2026-03-31';Because holiday_date has no time portion, March 31 is the whole stored value.
For a timestamp column, use the next period's start instead:
SELECT id, created_at
FROM orders
WHERE created_at >= DATE '2026-03-01'
AND created_at < DATE '2026-04-01';Many databases accept date strings without the type prefix, but typed literals such as DATE '2026-03-01' and TIMESTAMP '2026-04-01 00:00:00' make the intended type clearer. Exact literal syntax varies by database, so check the documentation for your SQL system.
Time zones and indexes
A timestamp with time-zone meaning must be compared in a consistent time zone. For example, “March 1 through March 31” in Berlin does not begin at the same instant as that period in UTC. Convert the boundaries to the time zone used by the stored values before filtering.
Keep the column unchanged on the left side of the comparison:
WHERE created_at >= TIMESTAMP '2026-03-01 00:00:00'
AND created_at < TIMESTAMP '2026-04-01 00:00:00'This form can usually use an index on created_at. Applying a function to the column, such as converting every row to a date, may prevent an ordinary index from being used.
Exercise
Write a condition that returns all events from April 2026 when events.occurred_at is a timestamp.
Show answer
WHERE occurredat >= TIMESTAMP '2026-04-01 00:00:00' AND occurredat < TIMESTAMP '2026-05-01 00:00:00';