sqlstreak

SQL function

SQLite julianday(): days between dates

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

Short answer

julianday(date) returns the date as a Julian day number, a decimal count of days, so julianday(d2) - julianday(d1) is the number of days between two dates. PostgreSQL doesn't need it: subtracting one date from another already gives days.

Syntax and availability

PostgreSQLSQLite
AvailableNo: subtract dates; to_char(d, 'J') gives a whole Julian day numberYes, built in
Syntaxd2 - d1 -- days between two datesjulianday(time_value [, modifier, …])

Examples

Days from joining to the first order

PostgreSQL

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

SQLite

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

Result (PostgreSQL and SQLite, identical): 5 rows

namedays
Marcus Bell135
Lena Fischer183
Sam Okafor230
Ellie Park263
Tomás Herrera307

Try it: PostgreSQL playground · SQLite playground

How long each waiting pet has waited (to 1 July 2026)

PostgreSQL

SELECT name, arrived_on,
       DATE '2026-07-01' - arrived_on AS days_waiting
FROM pets
WHERE adopted = false
ORDER BY days_waiting DESC
LIMIT 5;

SQLite

SELECT name, arrived_on,
       CAST(julianday('2026-07-01') - julianday(arrived_on) AS INTEGER) AS days_waiting
FROM pets
WHERE adopted = false
ORDER BY days_waiting DESC
LIMIT 5;

Result (PostgreSQL and SQLite, identical): 5 rows

namearrived_ondays_waiting
Tofu2025-04-04453
Gizmo2025-05-23404
Bruno2025-06-30366
Rocket2025-07-09357
Socks2025-08-17318

Try it: PostgreSQL playground · SQLite playground

Average days from sign-up to subscribing

PostgreSQL

SELECT ROUND(AVG(s.started_on - u.signed_up_on), 1) AS avg_days
FROM subscriptions s
JOIN users u ON u.id = s.user_id;

SQLite

SELECT ROUND(AVG(julianday(s.started_on) - julianday(u.signed_up_on)), 1) AS avg_days
FROM subscriptions s
JOIN users u ON u.id = s.user_id;

Result (PostgreSQL and SQLite, identical): 1 row

avg_days
10

Try it: PostgreSQL playground · SQLite playground

How it behaves: edge cases, measured

Each expression was run as SELECT … on PostgreSQL 18.6 and SQLite 3.54.1 when this page was built. Nothing here is typed by hand.

CasePostgreSQLSQLite
A dateto_char(DATE '2026-03-15', 'J')'2461115'julianday('2026-03-15')2461114.5
Days between two datesDATE '2026-03-15' - DATE '2026-03-01'14julianday('2026-03-15') - julianday('2026-03-01')14
Hours between two timestampsEXTRACT(EPOCH FROM TIMESTAMP '2026-03-15 18:00' - TIMESTAMP '2026-03-15 09:30') / 36008.5(julianday('2026-03-15 18:00') - julianday('2026-03-15 09:30')) * 248.49999999627471
Across a leap dayDATE '2024-03-01' - DATE '2024-02-28'2julianday('2024-03-01') - julianday('2024-02-28')2
Text that isn't a dateDATE 'soon' - DATE '2026-01-01'error: invalid input syntax for type date: "soon"julianday('soon')NULL
NULLNULL::date - DATE '2026-01-01'NULLjulianday(NULL)NULL

Common mistakes

  • Subtracting date text in SQLite: '2026-03-15' - '2026-03-01' subtracts the leading numbers and gives 0. Use julianday on both sides.

  • Forgetting the fraction: julianday('2026-03-15') ends in .5 (Julian days start at noon). Round or CAST the difference.

  • Using julianday in PostgreSQL: it doesn't exist; subtract the dates.

  • Trusting the decimals: julianday works in floating point, so 8.5 hours came back as 8.49999999627471 (see the matrix). ROUND the result.

PostgreSQL and SQLite

SQLite-only. In PostgreSQL, date minus date is already an integer number of days, and to_char(d, 'J') gives a whole Julian day number (without SQLite's .5, as the matrix shows).

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