Skip to main content

How to Find Rows from the Last N Days in SQLite with date()

· 5 min read
Jagdish Kumawat
Founder @ Dewiride

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​

Syntax
SELECT <columns> FROM <table>
WHERE <date column> >= date('now', '-<N> days');
PartWhat 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 --version to 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:

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 days_ago.sql script in a macOS-style code window: a SELECT of today, 7 days ago and 30 days ago, a loans table, Emma borrowed 3 days ago and Dune 20 days ago, and a SELECT of the loans on or after the date 14 days ago.

  • The first SELECT shows today, 7 days ago and 30 days ago. AS names 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 SELECT keeps the loans from the last 14 days.

Step 2: Run It​

In the folder that holds the script, run:

Terminal
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.

Output
+------------+------------+------------+
| today | week_ago | month_ago |
+------------+------------+------------+
| 2026-12-03 | 2026-11-26 | 2026-11-03 |
+------------+------------+------------+
+------+-------------+
| book | borrowed_on |
+------+-------------+
| Emma | 2026-11-30 |
+------+-------------+

The output in a terminal: today 2026-12-03, a week ago 2026-11-26 and 30 days ago 2026-11-03, then the one loan from the last 14 days, Emma borrowed on 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​

MistakeWhat happensDo this instead
No space in the modifier, '-7days'date('now', '-7days') returned NULLPut a space before the unit: '-7 days'
Using weeks, '-1 week'NULL: weeks aren't one of the unitsCount 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 aloneCompare the date part: date('2026-12-03 10:00:00') <= '2026-12-03' gave 1

Troubleshooting​

SymptomLikely causeFix
date() returns an empty cellA modifier SQLite doesn't recognize, so the result is NULLCheck the spelling and the space, as in '-7 days'
Parse error near line 5 of days_ago.sql: table loans already existsThe script already ran on library.dbDelete 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​