BETWEEN with a bare end date loses times on the last day, so end before the next start
Dates in SQLite are text, as the SQL window functions lesson showed: SQLite has no date or time type of its own. Here placed_at is a TEXT column, so comparing two values compares strings. Strings in one consistent ISO format sort in time order, so that mostly works, until the last day of a range.
The question: orders placed in March.
SELECT order_id, placed_at
FROM orders
WHERE placed_at BETWEEN '2026-03-01' AND '2026-03-31';
| order_id | placed_at |
|---|---|
| 10 | 2026-03-02 12:15 |
| 11 | 2026-03-02 19:40 |
| 12 | 2026-03-15 08:05 |
Order 13, at 20:30 on March 31, is missing. Here's where values sort as strings, and which ones each range keeps. BETWEEN here is BETWEEN '2026-03-01' AND '2026-03-31', and half-open is >= '2026-03-01' AND < '2026-04-01'.
| Value, in string order | BETWEEN | Half-open |
|---|---|---|
'2026-03-01' | in | in |
'2026-03-02 12:15' | in | in |
'2026-03-31' | in | in |
'2026-03-31 00:00' | out | in |
'2026-03-31 20:30' | out | in |
'2026-04-01' | out | out |
'2026-04-01 09:00' | out | out |
'2026-03-31 20:30' starts with '2026-03-31' and keeps going, so it sorts after the upper bound. So does every March 31 value with a time, even midnight:
SELECT '2026-03-31 20:30' <= '2026-03-31' AS evening,
'2026-03-31 00:00' <= '2026-03-31' AS midnight;
| evening | midnight |
|---|---|
| 0 | 0 |
PostgreSQL has real timestamp types and nearly the same bug. Its docs say a date compared with a timestamp counts as midnight. So the range stops at 00:00 on March 31 and drops the rest of the day.
In either engine, use a half-open range: on or after the start, and strictly before the next start. Here it is for one day, March 2, where BETWEEN '2026-03-02' AND '2026-03-02' finds nothing at all:
SELECT order_id, placed_at
FROM orders
WHERE placed_at >= '2026-03-02' AND placed_at < '2026-03-03';
| order_id | placed_at |
|---|---|
| 10 | 2026-03-02 12:15 |
| 11 | 2026-03-02 19:40 |
The same shape works for a week or a month, and you never need to know how many days the month has.
In the editor, fix MARCH_IDS: the March orders, the late evening of the 31st included.