date-time

INTERVAL arithmetic

Adds or subtracts a duration to a date/timestamp. Postgres uses the SQL-standard INTERVAL literal directly; MySQL wraps it in DATE_ADD/DATE_SUB; SQLite uses date modifiers.

timestamp + INTERVAL '1 day'  |  DATE_ADD(dt, INTERVAL 1 DAY)

Parameters / Modifiers

Parameter Purpose
'n days' / 'n hours' / 'n months' Postgres INTERVAL unit strings
DATE_ADD / DATE_SUB MySQL functions that accept INTERVAL n UNIT
datetime(dt, '+1 day') SQLite modifier syntax
AGE(a, b) Postgres companion returning an interval difference

Examples

SELECT NOW() + INTERVAL '7 days';

Postgres: timestamp one week from now

SELECT DATE_ADD(NOW(), INTERVAL 7 DAY);

MySQL: same result

SELECT datetime('now', '+7 days');

SQLite equivalent

SELECT * FROM sessions WHERE created_at > NOW() - INTERVAL '24 hours';

Postgres: rows created in the last day

Dialect notes / Gotcha

Adding 'INTERVAL 1 month' to Jan 31 lands on Feb 28/29, not Mar 3 — engines clamp to the last day of month. MySQL will not accept the Postgres-style 'INTERVAL '1 day'' literal outside DATE_ADD.

Related functions

← All SQL functions · SQL cheat sheet · Window functions · PostgreSQL cheat sheet