Query to include the last day
SELECT id, created_at
FROM event_dates_demo
WHERE created_at >= '2026-09-10 00:00:00.000'
AND created_at < '2026-09-13 00:00:00.000'
ORDER BY created_at, id;Set up the example data
An activity report must include every event logged during those three days. This made-up data adds rows just before the start, at both edges and with no date at all. Those are the cases that expose a wrong filter even when it seems to work with ordinary records.
Run the setup only once on a SQLite test connection. We store the text with the same format, precision and time zone so that sorting it as a string matches chronological order. In an application, enforce that rule when the data is saved.
CREATE TABLE event_dates_demo (
id INTEGER PRIMARY KEY,
created_at TEXT
);
INSERT INTO event_dates_demo (id, created_at) VALUES
(1, '2026-09-09 23:59:59.999'),
(2, '2026-09-10 00:00:00.000'),
(3, '2026-09-12 14:30:00.000'),
(4, '2026-09-12 23:59:59.999'),
(5, '2026-09-13 00:00:00.000'),
(6, NULL);| id | created_at |
|---|---|
| 2 | 2026-09-10 00:00:00.000 |
| 3 | 2026-09-12 14:30:00.000 |
| 4 | 2026-09-12 23:59:59.999 |
Row 1 falls before the start. Row 2 is included by the comparison >=. Rows 3 and 4 belong to the last day requested. Row 5 already belongs to the next day and is left out. Row 6 contains NULL: the comparisons are not true, so the filter discards it.
How it works
BETWEEN includes both ends. For a DATE column with no time, a range from the 10th to the 12th covers exactly those three days. The problem appears when you use the same end bound on a column that also stores times: midnight on the 12th does not cover the rest of the 12th.
SELECT id, created_at
FROM event_dates_demo
WHERE created_at BETWEEN '2026-09-10 00:00:00.000'
AND '2026-09-12 00:00:00.000'
ORDER BY created_at, id;With our data, this query returns row 2 only. Rows 3 and 4 come after midnight on the 12th. Adding 23:59:59 does not cover every possible fraction of a second either. Using the next day as an exclusive bound means you do not depend on precision.
| Need | Bounds | Details |
|---|---|---|
| Whole days on a DATE column | Start and end both inclusive | BETWEEN can work if there is no time part. |
| Whole days on a date-and-time column | Inclusive start; exclusive next day | Includes every hour of the last day. |
| Shift from 09:00 to 17:00 | From 09:00, up to but not including 17:00 | The 17:00 event belongs to the next shift. |
| Two exact points in time, both included | From the first to the second, inclusive | BETWEEN works if those two points in time really are the bounds. |
Engines, indexes and time zones
How to write it in PostgreSQL, MySQL, SQL Server and SQLite
The queries below apply the same period to a table in your own application called events. They are reference versions for date-and-time columns with no time zone conversion; not all of them run in SQLite. In application code, bind parameters with the right type through your driver instead of concatenating user input.
PostgreSQL: timestamp column without time zone
SELECT id, created_at
FROM events
WHERE created_at >= TIMESTAMP '2026-09-10 00:00:00'
AND created_at < TIMESTAMP '2026-09-13 00:00:00'
ORDER BY created_at, id;If the column is timestamp with time zone, the bounds must represent the start and end of the period in the business time zone. PostgreSQL distinguishes the two types and how each is interpreted: see its date and time documentation (opens in a new tab).
MySQL: DATETIME column
SELECT id, created_at
FROM events
WHERE created_at >= CAST('2026-09-10 00:00:00' AS DATETIME)
AND created_at < CAST('2026-09-13 00:00:00' AS DATETIME)
ORDER BY created_at, id;The conversion is applied to the constants. For a TIMESTAMPcolumn, also check the session time zone. MySQL's date and time literals (opens in a new tab) and its types define how input values are interpreted.
SQL Server: datetime2 column
DECLARE @start datetime2(3) = '2026-09-10T00:00:00.000';
DECLARE @exclusive_end datetime2(3) = '2026-09-13T00:00:00.000';
SELECT id, created_at
FROM events
WHERE created_at >= @start
AND created_at < @exclusive_end
ORDER BY created_at, id;The separator T makes the ISO 8601 format accepted by datetime2 (opens in a new tab)explicit. If the values carry time zones or offsets, review the model with datetimeoffset.
SQLite: consistent text or numeric timestamps
The first example uses consistently formatted text. If you store Unix timestamps, compare them with numeric bounds in the same unit: seconds and milliseconds are not interchangeable. Do not mix text with spaces, with T or with different offsets and expect string comparison to reflect the same points in time. SQLite documents the supported formats in its date and time functions (opens in a new tab).
Indexes, parameters and time zones
A plain index on created_at can help when you compare the column directly with the two bounds. Wrapping every row in DATE(created_at) changes the expression the engine has to evaluate; it may need a different plan or an index on that expression. A range is not guaranteed to avoid a full scan or to always be faster: check the plan with EXPLAIN and representative data.
For a report covering a local day with data stored in UTC, work out the two local midnights and convert them to UTC before querying. A calendar day is not always 24 hours long when the clocks change, so do not get the end by adding 86,400 seconds without allowing for that.
If a screen receives “up to the 12th”, turn that day into the start of the 13th in the agreed time zone, and document that the end parameter is exclusive. That way consecutive periods can be reused without counting a row on the boundary twice.
How to check it
SELECT id, created_at
FROM event_dates_demo
WHERE created_at >= '2026-09-12 00:00:00.000'
AND created_at < '2026-09-13 00:00:00.000'
ORDER BY created_at, id;The start includes midnight on the 12th; the end excludes midnight on the 13th. The same pattern works for a day, a week or any range a user picks, as long as you work out the boundaries correctly.
More date filter cases
Date filter FAQ
How do I include records with no date?
Add OR created_at IS NULL to the group of range conditions. If there are other filters, such as a customer or a status, wrap the range and that alternative in parentheses so they are applied together. Only include undated records if the report needs them.
How do I compare two date columns?
If the types and the way they handle time are compatible, use a condition such as end >= start. That checks how the values are ordered. To work out how many days or hours apart they are, see the guide on date differences.
Can I compare dates stored as DD/MM/YYYY?
Do not rely on their order as text: the day comes before the year. Convert and validate the data with the engine's functions, then store it as a date type or in a consistent format. Changing how dates are displayed does not necessarily change how they are stored.