SQL Date Arithmetic
Add or subtract a number of days directly from a date using plain + and - operators — dates behave like numbers in this specific way.
What & Why
Postgres lets you add or subtract a plain integer directly to or from a DATE, and it's interpreted as a number of days. start_date + 7 means "seven days after start_date." This is the simplest form of date math — the INTERVAL lesson right after this one covers a more flexible, explicit way to express the same kind of arithmetic.
See How It Works
Marketing wants seven-day and thirty-day checkpoints derived from every campaign start date.
| id | name | channel | spend | start_date | end_date | status | target_segment |
|---|---|---|---|---|---|---|---|
| 1 | Spring Launch | google_ads | 55000.00 | 2024-01-15 | 2024-03-31 | active | smb |
| 2 | Retention Webinar | 45000.00 | 2024-02-10 | 2024-04-15 | active | enterprise | |
| 3 | Finance Retargeting | 50000.00 | 2024-03-12 | 2024-05-31 | active | enterprise | |
| 4 | Enterprise Search | google_ads | 60000.00 | 2024-04-01 | 2024-06-30 | active | enterprise |
| id | name | start_date | plus_7_days |
|---|---|---|---|
| 1 | Spring Launch | 2024-01-15 | 2024-01-22 |
| 2 | Retention Webinar | 2024-02-10 | 2024-02-17 |
| 3 | Finance Retargeting | 2024-03-12 | 2024-03-19 |
| 4 | Enterprise Search | 2024-04-01 | 2024-04-08 |
Adding the integer 7 moves every displayed campaign DATE forward by exactly seven days.
SELECT
id,
name,
start_date,
start_date + 7 AS plus_7_days,
start_date - 7 AS minus_7_days,
start_date + 30 AS plus_30_days
FROM marketing.campaigns
ORDER BY id;Practice this concept
Marketing wants seven-day and thirty-day checkpoints derived from every campaign start date.
marketingPrefix tables with marketing.table_name.
idnamestart_dateplus_7_daysminus_7_daysplus_30_daysmarketing.campaigns| Column | Type |
|---|---|
| id | integer |
| name | text |
| channel | text |
| spend | numeric |
| start_date | date |
| end_date | date |
| status | text |
| target_segment | text |
| legacy_id | text |
Sign up free to try it on a real business scenario