How to Store Dates in SQLite as ISO 8601 Text
SQLite has no date type. The usual way to store a date is as text in the ISO 8601 form, year first: 2026-12-15. Text in that form sorts and compares in date order. This guide stores three events and sorts and filters them by date, with real output from SQLite 3.54.1.
Syntax
INSERT INTO <table> (<column>) VALUES ('YYYY-MM-DD');
SELECT <columns> FROM <table> ORDER BY <column>;
| Part | What it means |
|---|---|
'YYYY-MM-DD' | The date as text: a four-digit year, a two-digit month and a two-digit day, joined by dashes, such as '2026-12-15' |
ORDER BY <column> | Sorts by the column. Dates in this form sort in date order |
SQLite's documentation on dates: "SQLite does not have a storage class set aside for storing dates and/or times." Its date functions read them as text, among other forms: "TEXT as ISO8601 strings ("YYYY-MM-DD HH:MM:SS.SSS")."
Prerequisites
- The sqlite3 command-line shell. This guide uses SQLite 3.54.1. Run
sqlite3 --versionto see yours; the SQLite download page has the command-line tools for Windows, macOS and Linux.
Step 1: Write the Script
Save this as dates.sql:
CREATE TABLE events (
name TEXT NOT NULL,
held_on TEXT NOT NULL
);
INSERT INTO events (name, held_on) VALUES ('Book fair', '2026-12-15');
INSERT INTO events (name, held_on) VALUES ('Quiz night', '2025-11-30');
INSERT INTO events (name, held_on) VALUES ('Board games', '2026-02-01');
SELECT name, held_on FROM events ORDER BY held_on;
SELECT name, held_on FROM events WHERE held_on >= '2026-01-01';

held_on TEXT NOT NULLholds each date as text.- Every date is written year first, with two-digit months and days.
ORDER BY held_onsorts the events by date.WHERE held_on >= '2026-01-01'keeps the events on or after 1 January 2026.
Step 2: Run It
In the folder that holds the script, run:
sqlite3 -table events.db ".read dates.sql"
sqlite3 opens the database file events.db, and creates it when it isn't there. -table prints each result as a table, and .read runs the statements in the script. The command is the same in bash, zsh and PowerShell.
+-------------+------------+
| name | held_on |
+-------------+------------+
| Quiz night | 2025-11-30 |
| Board games | 2026-02-01 |
| Book fair | 2026-12-15 |
+-------------+------------+
+-------------+------------+
| name | held_on |
+-------------+------------+
| Book fair | 2026-12-15 |
| Board games | 2026-02-01 |
+-------------+------------+

ORDER BY put the events in date order: Quiz night in 2025, then Board games and Book fair in 2026. The WHERE kept the two in 2026. Both work because SQLite compares the text character by character, and dates written year first, all the same length, compare like dates.
Common Mistakes
| Mistake | What happens | Do this instead |
|---|---|---|
Writing dates month first, as '12/15/2026' | They sorted as 02/01/2026, 11/30/2025, 12/15/2026, and date('12/15/2026') returned NULL: SQLite's date functions don't read that form | Write the year first: '2026-12-15' |
Leaving out a zero, as '2026-12-3' | date('2026-12-3') returned NULL, and '2026-12-3' > '2026-12-15' came out true | Always two digits: '2026-12-03' |
Troubleshooting
| Symptom | Likely cause | Fix |
|---|---|---|
| A date function returns an empty cell | The text isn't in a form SQLite's date functions read, so they return NULL | Store dates as YYYY-MM-DD |
Parse error near line 1 of dates.sql: table events already exists | The script already ran on events.db | Delete events.db and run the script again |
FAQ
How do I store a date with a time?
As 'YYYY-MM-DD HH:MM:SS'. The date functions read that form too: datetime('2026-12-15 18:30:00', '+2 hours') returned 2026-12-15 20:30:00.
Does SQLite check that a date is real?
No. date('2026-02-30') returned 2026-03-02, moving the extra days into March. Check dates before you save them.
Conclusion
Stored as year-first text, the three dates sorted into date order with ORDER BY held_on, and >= '2026-01-01' picked out the ones in 2026.
Additional Resources
- Date and time datatype — the ways SQLite can store dates.
- Date And Time Functions — the forms the date functions read, and what they can calculate.
