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
| PostgreSQL | SQLite | |
|---|---|---|
| Available | No: subtract dates; to_char(d, 'J') gives a whole Julian day number | Yes, built in |
| Syntax | d2 - d1 -- days between two dates | julianday(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
| name | days |
|---|---|
| Marcus Bell | 135 |
| Lena Fischer | 183 |
| Sam Okafor | 230 |
| Ellie Park | 263 |
| Tomás Herrera | 307 |
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
| name | arrived_on | days_waiting |
|---|---|---|
| Tofu | 2025-04-04 | 453 |
| Gizmo | 2025-05-23 | 404 |
| Bruno | 2025-06-30 | 366 |
| Rocket | 2025-07-09 | 357 |
| Socks | 2025-08-17 | 318 |
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.
| Case | PostgreSQL | SQLite |
|---|---|---|
| A date | to_char(DATE '2026-03-15', 'J')'2461115' | julianday('2026-03-15')2461114.5 |
| Days between two dates | DATE '2026-03-15' - DATE '2026-03-01'14 | julianday('2026-03-15') - julianday('2026-03-01')14 |
| Hours between two timestamps | EXTRACT(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 day | DATE '2024-03-01' - DATE '2024-02-28'2 | julianday('2024-03-01') - julianday('2024-02-28')2 |
| Text that isn't a date | DATE 'soon' - DATE '2026-01-01'error: invalid input syntax for type date: "soon" | julianday('soon')NULL |
| NULL | NULL::date - DATE '2026-01-01'NULL | julianday(NULL)NULL |
Related
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
- Guide: SQL date functions in PostgreSQL and SQLite
- Practice: SQL date practice: 7 exercises in PostgreSQL and SQLite
- Lesson: Date maths (Pro)
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.