How to Find Rows from the Last N Days in SQLite with date()
Which books were borrowed in the last two weeks? date('now', '-14 days') gives the date 14 days ago, and a WHERE keeps the rows on or after it. This guide finds the recent loans in a small library, with real output from SQLite 3.54.1.
Syntax
SELECT <columns> FROM <table>
WHERE <date column> >= date('now', '-<N> days');
| Part | What it means |
|---|---|
date('now', '-<N> days') | Today's date, moved back N days. '+<N> days' moves it forward |
'-<N> days' | A modifier: text that changes the date to its left |
<date column> >= ... | Keeps the rows on or after that date. The column holds dates as YYYY-MM-DD text |
SQLite's documentation on modifiers: "The first thirteen modifiers (1 through 13) add the specified amount of time to the date and time specified by the arguments to its left."
Prerequisites
- The sqlite3 command-line shell. This guide uses SQLite 3.54.1. Run
sqlite3 --versionto see yours; the SQLite download page has the command-line tools for Windows, macOS and Linux.
Step 1: Write the Script
Save this as days_ago.sql:
SELECT date('now') AS today,
date('now', '-7 days') AS week_ago,
date('now', '-30 days') AS month_ago;
CREATE TABLE loans (
book TEXT NOT NULL,
borrowed_on TEXT NOT NULL
);
INSERT INTO loans (book, borrowed_on) VALUES ('Emma', date('now', '-3 days'));
INSERT INTO loans (book, borrowed_on) VALUES ('Dune', date('now', '-20 days'));
SELECT book, borrowed_on FROM loans WHERE borrowed_on >= date('now', '-14 days');

- The first
SELECTshows today, 7 days ago and 30 days ago.ASnames each result column. - The loan dates are worked out with
date('now', ...)too, so the example has recent dates whenever it runs: Emma 3 days ago, Dune 20 days ago. - The last
SELECTkeeps the loans from the last 14 days.
Step 2: Run It
In the folder that holds the script, run:
sqlite3 -table library.db ".read days_ago.sql"
sqlite3 opens the database file library.db, and creates it when it isn't there. -table prints each result as a table, and .read runs the statements in the script. The command is the same in bash, zsh and PowerShell.
+------------+------------+------------+
| today | week_ago | month_ago |
+------------+------------+------------+
| 2026-12-03 | 2026-11-26 | 2026-11-03 |
+------------+------------+------------+
+------+-------------+
| book | borrowed_on |
+------+-------------+
| Emma | 2026-11-30 |
+------+-------------+

On 3 December 2026, 7 days back was 26 November and 30 days back was 3 November. Emma, borrowed on 30 November, is within the last 14 days, and Dune isn't. Your run shows dates counted from your own today.
Common Mistakes
| Mistake | What happens | Do this instead |
|---|---|---|
No space in the modifier, '-7days' | date('now', '-7days') returned NULL | Put a space before the unit: '-7 days' |
Using weeks, '-1 week' | NULL: weeks aren't one of the units | Count in days: '-7 days' |
| Comparing a date-and-time column with a date | '2026-12-03 10:00:00' <= '2026-12-03' gave 0: the longer text sorts after the date alone | Compare the date part: date('2026-12-03 10:00:00') <= '2026-12-03' gave 1 |
Troubleshooting
| Symptom | Likely cause | Fix |
|---|---|---|
date() returns an empty cell | A modifier SQLite doesn't recognize, so the result is NULL | Check the spelling and the space, as in '-7 days' |
Parse error near line 5 of days_ago.sql: table loans already exists | The script already ran on library.db | Delete library.db and run the script again |
FAQ
Is it "days" or "day"?
Either: '-1 day' and '-1 days' gave the same date. The documentation: "The 's' character at the end of the modifier names in 1 through 6 is optional." Capitals work too: '-7 DAYS' gave a date.
Can I go back a month?
Yes, with '-1 month', but watch the ends of months. date('2026-03-31', '-1 month') returned 2026-03-03, because there's no 31 February and the extra days roll into March. Adding 'floor' returned 2026-02-28. The documentation: "The "floor" modifier resolves ambiguities by resolving to the last day of the previous month."
Conclusion
date('now', '-14 days') gave the date two weeks back, and WHERE borrowed_on >= it kept Emma's loan from 3 days ago and left out Dune's from 20 days ago.
Additional Resources
- Modifiers — every unit, plus
floor,ceilingandstart of month. - Date And Time Functions —
date(),datetime()and the rest.
