Skip to main content

How to Store Dates in SQLite as ISO 8601 Text

· 5 min read
Jagdish Kumawat
Founder @ Dewiride

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​

Syntax
INSERT INTO <table> (<column>) VALUES ('YYYY-MM-DD');
SELECT <columns> FROM <table> ORDER BY <column>;
PartWhat 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 --version to 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:

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';

The dates.sql script in a macOS-style code window: an events table with a TEXT held_on column, three events dated 2026-12-15, 2025-11-30 and 2026-02-01, a SELECT ordered by held_on, and a SELECT of the events on or after 2026-01-01.

  • held_on TEXT NOT NULL holds each date as text.
  • Every date is written year first, with two-digit months and days.
  • ORDER BY held_on sorts 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:

Terminal
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.

Output
+-------------+------------+
| 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 |
+-------------+------------+

The output in a terminal: the events in date order, Quiz night on 2025-11-30, Board games on 2026-02-01 and Book fair on 2026-12-15, then the two events in 2026.

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​

MistakeWhat happensDo 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 formWrite 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 trueAlways two digits: '2026-12-03'

Troubleshooting​

SymptomLikely causeFix
A date function returns an empty cellThe text isn't in a form SQLite's date functions read, so they return NULLStore dates as YYYY-MM-DD
Parse error near line 1 of dates.sql: table events already existsThe script already ran on events.dbDelete 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​