9. Date and Time Queries for Analysts
9.3 How to Compute Date Differences (Cheque Clearing Delay Story)
Why. Customers complain when a cheque takes too long to clear. The bank needs the number of days between two dates.
What. julianday(date) turns a date into a day number. Subtracting two julianday values gives the number of days between them. date(value, '+3 days') moves a date forward, which is useful for due dates.
How. For each cheque, subtract the posting date from the clearing date, and also show a promised date three days after posting.
Steps
- Run the first example to see days_to_clear for every cheque.
- Notice that a cheque with no clearing date shows NULL, because it has not cleared yet.
- Run the second example to see the promised date three days after posting.
- Use date(posted_at) inside julianday() so that the time of day does not add part of a day.
Examples
Example 1 · dataset: northstar_bank
SELECT txn_id,
date(posted_at) AS posted_date,
cleared_on,
julianday(cleared_on) - julianday(date(posted_at)) AS days_to_clear
FROM transactions
WHERE txn_type = 'cheque'
ORDER BY txn_id;Output
| txn_id | posted_date | cleared_on | days_to_clear |
|---|---|---|---|
| 2 | 2026-08-19 | 2026-08-21 | 2.0 |
| 4 | 2026-08-28 | 2026-09-02 | 5.0 |
| 6 | 2026-08-30 | 2026-09-01 | 2.0 |
| 9 | 2026-09-14 | 2026-09-18 | 4.0 |
| 13 | 2026-09-25 | 2026-09-29 | 4.0 |
| 15 | 2026-09-29 | ||
| 18 | 2026-10-20 | 2026-10-23 | 3.0 |
| 20 | 2026-10-27 | ||
| 24 | 2026-10-31 | 2026-11-02 | 2.0 |
Meaning. Most cheques clear in 2 to 5 days. Two cheques show NULL because they have not cleared yet.
Example 2 · dataset: northstar_bank
SELECT txn_id,
date(posted_at) AS posted_date,
date(posted_at, '+3 days') AS promised_by,
cleared_on
FROM transactions
WHERE txn_type = 'cheque'
AND cleared_on IS NOT NULL
ORDER BY txn_id;Output
| txn_id | posted_date | promised_by | cleared_on |
|---|---|---|---|
| 2 | 2026-08-19 | 2026-08-22 | 2026-08-21 |
| 4 | 2026-08-28 | 2026-08-31 | 2026-09-02 |
| 6 | 2026-08-30 | 2026-09-02 | 2026-09-01 |
| 9 | 2026-09-14 | 2026-09-17 | 2026-09-18 |
| 13 | 2026-09-25 | 2026-09-28 | 2026-09-29 |
| 18 | 2026-10-20 | 2026-10-23 | 2026-10-23 |
| 24 | 2026-10-31 | 2026-11-03 | 2026-11-02 |
Meaning. promised_by is three days after posting. Any row where cleared_on is later than promised_by missed the promise.
Ravindra Bagale's Tip
julianday() can return a decimal when times are included. Wrap date-time text in date() when you only want whole days.
Ravindra Bagale's Tip – मराठी
Times सोबत असतील तर julianday() दशांश आकडा देऊ शकतं. फक्त पूर्ण दिवस हवे असतील तर date-time text date() मध्ये गुंडाळा.
Ravindra Bagale's Tip – हिंदी
Times साथ हों तो julianday() दशमलव वाला आंकड़ा दे सकता है। सिर्फ़ पूरे दिन चाहिए हों तो date-time text को date() में लपेटिए।