SQL Subtract Dates

Subtracting one DATE from another gives you a plain number — the count of days between them, not a date.

What & Why

date1 - date2 returns an integer: the number of days separating the two dates. This is genuinely different from subtracting a number FROM a date (which gives another date) — subtracting two dates FROM each other gives a plain count.

See How It Works

BUSINESS QUESTION

Marketing wants the duration of completed campaigns.

idnamechannelspendstart_dateend_datestatustarget_segment
1Spring Launchgoogle_ads55000.002024-01-152024-03-31activesmb
2Retention Webinaremail45000.002024-02-102024-04-15activeenterprise
3Finance Retargetinglinkedin50000.002024-03-122024-05-31activeenterprise
4Enterprise Searchgoogle_ads60000.002024-04-012024-06-30activeenterprise
Watch DATE subtraction produce elapsed daysExample 1 of 4
end_date - start_date
idnamestart_dateend_dateduration_days
1Spring Launch2024-01-152024-03-3176
2Retention Webinar2024-02-102024-04-15?
3Finance Retargeting2024-03-122024-05-31?
4Enterprise Search2024-04-012024-06-30?

The later date minus the earlier date produces Spring Launch's duration.

EXAMPLE QUERY
SELECT
  id,
  name,
  start_date,
  end_date,
  end_date - start_date AS duration_days
FROM marketing.campaigns
WHERE end_date IS NOT NULL
ORDER BY id;
Now You Try

Practice this concept

Marketing wants the elapsed day count for every completed campaign in the same order as the displayed source table.

Available schema
marketing

Prefix tables with marketing.table_name.

idnamestart_dateend_dateduration_days
marketing.campaigns
ColumnType
idinteger
nametext
channeltext
spendnumeric
start_datedate
end_datedate
statustext
target_segmenttext
legacy_idtext
query.sql
Intermediate business practice

Sign up free to try it on a real business scenario