sqlstreak

SQL how-to

Date difference in SQL: PostgreSQL and SQLite

Updated · Tested on PostgreSQL 18.6 and SQLite 3.54.1 (results generated , re-checked on every release)

Short answer

PostgreSQL subtracts dates directly: d2 - d1 is a number of days. SQLite turns dates into day numbers first: julianday(d2) - julianday(d1). Neither has DATEDIFF, which belongs to MySQL and SQL Server.

The task: Find how many days (or months) lie between two dates.

Days from joining to the first order

PostgreSQL

SELECT c.name, c.joined_on, MIN(o.ordered_on) AS first_order,
       MIN(o.ordered_on) - c.joined_on AS days_to_order
FROM customers c
JOIN orders o ON o.customer_id = c.id
GROUP BY c.id, c.name, c.joined_on
ORDER BY days_to_order;

SQLite

SELECT c.name, c.joined_on, MIN(o.ordered_on) AS first_order,
       CAST(julianday(MIN(o.ordered_on)) - julianday(c.joined_on) AS INTEGER) AS days_to_order
FROM customers c
JOIN orders o ON o.customer_id = c.id
GROUP BY c.id, c.name, c.joined_on
ORDER BY days_to_order;

Result (PostgreSQL and SQLite, identical): 8 rows

namejoined_onfirst_orderdays_to_order
Marcus Bell2025-10-302026-03-14135
Lena Fischer2025-09-092026-03-11183
Sam Okafor2025-07-212026-03-08230
Ellie Park2025-06-152026-03-05263
Tomás Herrera2025-05-022026-03-05307
Priya Nair2025-03-192026-03-04350
Jonah Reed2025-03-042026-03-01362
Maya Ortiz2025-02-112026-03-01383

Try it: PostgreSQL playground · SQLite playground

How it works

  1. 1
    MIN(o.ordered_on) - c.joined_on

    PostgreSQL: date minus date is a whole number of days.

  2. 2
    julianday(…) - julianday(…)

    SQLite: julianday turns a date into a day number; CAST(… AS INTEGER) drops the fraction.

More examples

Whole months between two dates

Count calendar months, minus one when the day of the month hasn't come round yet.

PostgreSQL

SELECT name, hired_on,
  EXTRACT(YEAR FROM age(DATE '2026-11-22', hired_on)) * 12
  + EXTRACT(MONTH FROM age(DATE '2026-11-22', hired_on)) AS months_employed
FROM staff
ORDER BY hired_on;

SQLite

SELECT name, hired_on,
  (strftime('%Y', '2026-11-22') - strftime('%Y', hired_on)) * 12
  + (strftime('%m', '2026-11-22') - strftime('%m', hired_on))
  - (strftime('%d', '2026-11-22') < strftime('%d', hired_on)) AS months_employed
FROM staff
ORDER BY hired_on;

Result (PostgreSQL and SQLite, identical): 8 rows

namehired_onmonths_employed
Rosa Vega2024-01-1034
Omar Haddad2024-03-0132
Ben Carter2024-04-2031
Lia Moreau2024-06-1529
Ivan Petrov2024-08-0127
Yuki Tanaka2025-02-0121
Zara Ali2025-05-1218
Noah Kim2025-09-3013

Try it: PostgreSQL playground · SQLite playground

DATEDIFF fails in both

SELECT DATEDIFF('2026-03-31', ordered_on) FROM orders;

PostgreSQLERROR: function datediff(unknown, date) does not exist

SQLiteno such function: DATEDIFF

Try it: PostgreSQL playground · SQLite playground

Variations

Difference from today

Use CURRENT_DATE - d in PostgreSQL and julianday('now') - julianday(d) in SQLite. The examples use a fixed date so the results on this page don't change daily.

Hours or minutes between timestamps

PostgreSQL: EXTRACT(EPOCH FROM t2 - t1) / 3600. SQLite: (julianday(t2) - julianday(t1)) * 24.

Common mistakes

  • Subtracting date text in SQLite: '2026-03-31' - '2026-03-01' subtracts the leading numbers (2026 - 2026) and gives 0.

  • Dividing days by 30 to get months: months have 28 to 31 days. Count calendar months instead.

  • Using DATEDIFF, DATE_DIFF or TIMESTAMPDIFF: those are other databases' functions.

PostgreSQL and SQLite

This is the area where the two differ most: date arithmetic and age() in PostgreSQL, julianday and strftime in SQLite. Every example shows both forms, and each pair returned the same numbers.

Official documentation

Go deeper

Make it stick in five minutes a day

“Date maths” teaches this step by step on a real database, with instant feedback. Part of sqlstreak Pro; the first lessons are free.

Start free