sqlstreak

SQL function

SQLite date(): add days and months, start and end of month

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

Short answer

date(value, modifier, …) returns a date as 'YYYY-MM-DD' text after applying the modifiers in order: date('2026-03-15', '+7 days') is '2026-03-22'. PostgreSQL does the same with date arithmetic: DATE '2026-03-15' + 7.

Syntax and availability

PostgreSQLSQLite
AvailableNo date() function: use date arithmetic and date_truncYes, built in
SyntaxDATE '2026-03-15' + 7 / d + INTERVAL '1 month' / date_trunc('month', d)::datedate(time_value [, modifier, …]) -- modifiers: '+N days', '+N months', 'start of month', 'weekday N', …

Examples

A trial that ends 30 days after sign-up

PostgreSQL

SELECT id, signed_up_on,
       signed_up_on + 30 AS trial_ends
FROM users
ORDER BY id
LIMIT 5;

SQLite

SELECT id, signed_up_on,
       date(signed_up_on, '+30 days') AS trial_ends
FROM users
ORDER BY id
LIMIT 5;

Result (PostgreSQL and SQLite, identical): 5 rows

idsigned_up_ontrial_ends
12026-01-012026-01-31
22026-01-022026-02-01
32026-01-022026-02-01
42026-01-022026-02-01
52026-01-032026-02-02

Try it: PostgreSQL playground · SQLite playground

Group by the first day of the month

PostgreSQL

SELECT date_trunc('month', played_on)::date AS month_start, COUNT(*) AS plays
FROM plays
GROUP BY month_start
ORDER BY month_start;

SQLite

SELECT date(played_on, 'start of month') AS month_start, COUNT(*) AS plays
FROM plays
GROUP BY month_start
ORDER BY month_start;

Result (PostgreSQL and SQLite, identical): 4 rows

month_startplays
2026-01-01274
2026-02-01635
2026-03-011015
2026-04-01483

Try it: PostgreSQL playground · SQLite playground

The last day of each hire month

PostgreSQL

SELECT name, hired_on,
       (date_trunc('month', hired_on) + INTERVAL '1 month - 1 day')::date AS month_end
FROM staff
ORDER BY hired_on
LIMIT 5;

SQLite

SELECT name, hired_on,
       date(hired_on, 'start of month', '+1 month', '-1 day') AS month_end
FROM staff
ORDER BY hired_on
LIMIT 5;

Result (PostgreSQL and SQLite, identical): 5 rows

namehired_onmonth_end
Rosa Vega2024-01-102024-01-31
Omar Haddad2024-03-012024-03-31
Ben Carter2024-04-202024-04-30
Lia Moreau2024-06-152024-06-30
Ivan Petrov2024-08-012024-08-31

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
Plus 7 daysDATE '2026-03-15' + 7'2026-03-22'date('2026-03-15', '+7 days')'2026-03-22'
Plus 1 month from 31 January(DATE '2026-01-31' + INTERVAL '1 month')::date'2026-02-28'date('2026-01-31', '+1 month')'2026-03-03'
Start of monthdate_trunc('month', DATE '2026-03-15')::date'2026-03-01'date('2026-03-15', 'start of month')'2026-03-01'
Last day of February 2024(date_trunc('month', DATE '2024-02-10') + INTERVAL '1 month - 1 day')::date'2024-02-29'date('2024-02-10', 'start of month', '+1 month', '-1 day')'2024-02-29'
Next Monday on or afternot availabledate('2026-03-15', 'weekday 1')'2026-03-16'
30 FebruaryDATE '2026-02-30'error: date/time field value out of range: "2026-02-30"date('2026-02-30')'2026-03-02'
Text that isn't a dateDATE 'March 1'error: invalid input syntax for type date: "March 1"date('March 1')NULL
A timestamp loses its timeTIMESTAMP '2026-03-15 23:59:59'::date'2026-03-15'date('2026-03-15 23:59:59')'2026-03-15'
SQLite's modifiers in PostgreSQLdate('2026-03-15', '+7 days')error: function date(unknown, unknown) does not existnot available

Common mistakes

  • Adding a month to the 29th, 30th or 31st: SQLite and PostgreSQL disagree (see the matrix). Go to the start of the month first if that matters.

  • Putting modifiers in the wrong order: they apply left to right.

  • Using SQLite's modifiers in PostgreSQL: date(d, '+7 days') fails there (see the matrix). Add an integer or an INTERVAL instead.

PostgreSQL and SQLite

SQLite's date() with modifiers has no direct PostgreSQL twin; PostgreSQL adds integers and intervals to dates and truncates with date_trunc. The matrix shows where results differ: month overflow, invalid days (SQLite returns NULL or rolls over, PostgreSQL raises an error).

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