← Back to SQL, page 8

sql-0152 · Easy fresher

SQLite has no DATE type. How are dates usually stored there, and what are the trade-offs?

SQLite stores dates as ISO-8601 TEXT ('2026-03-01'), as REAL Julian day numbers, or as INTEGER Unix seconds, and its date functions accept all three. ISO text is readable and sorts correctly as a string. Integers are compact and fast to compare but need conversion to read.

Full answer
sql
SELECT date('2026-03-01')                AS from_text,
       date(2461100.5)                     AS from_julian,
       date(1772323200, 'unixepoch')       AS from_unix,
       julianday('2026-03-01')             AS as_julian,
       CAST(strftime('%s', '2026-03-01') AS INTEGER) AS as_unix;
from_textfrom_julianfrom_unixas_julianas_unix
2026-03-012026-03-012026-03-012461100.51772323200

A column declared DATE in SQLite gets NUMERIC affinity, and a value like '2026-03-01' stays TEXT because it does not look like a number. The functions date(), datetime(), julianday() and strftime() read all three forms, as long as integers are flagged with the 'unixepoch' modifier.

Choose one form per column and keep it. ISO text works with string comparison only if every value uses the same zero-padded format: '2026-03-01', never '2026-3-1'. PostgreSQL has real date, timestamp and timestamptz types that validate input, which SQLite does not do.

sql
SELECT date('2026-03-01')                AS from_text,
       date(2461100.5)                     AS from_julian,
       date(1772323200, 'unixepoch')       AS from_unix,
       julianday('2026-03-01')             AS as_julian,
       CAST(strftime('%s', '2026-03-01') AS INTEGER) AS as_unix;

Checked on SQLite 3.46.1, run twice with identical output.

Key points

  • Common forms: ISO TEXT, REAL Julian day, INTEGER Unix time
  • Date functions accept all three
  • ISO text sorts correctly only with consistent zero-padding
  • SQLite does not validate dates; PostgreSQL does

Common mistakes

  • Mixing formats in one column
  • Storing '3/1/2026' style text and sorting it

Follow-ups

  • How would you enforce a valid date format with a CHECK?
  • Which form would you pick for event timestamps?

Tips

  • Mention that SQLite accepts invalid dates like '2026-02-30' as text