← Back to SQL, page 8

sql-0155 · Hard senior

What does adding one month to 2026-01-31 return on SQLite and on PostgreSQL?

SQLite's '+1 month' modifier moves to 2026-02-31, which does not exist, and normalizes it forward to 2026-03-03. PostgreSQL's date + interval '1 month' clamps to the last valid day, 2026-02-28. Month arithmetic on month-end dates needs an explicit rule.

Full answer
sql
SELECT date('2026-01-31', '+1 month') AS plus_one_month;
plus_one_month
2026-03-03
sql
SELECT (DATE '2026-01-31' + INTERVAL '1 month')::date AS plus_one_month;
plus_one_month
2026-02-28

SQLite adds to the month field and then normalizes the impossible date by rolling the extra days into the next month. PostgreSQL clamps to the end of the shorter month. Neither is wrong by the standard, but billing code that assumes one behaviour will drift on the other.

Since SQLite 3.46, modifiers ceiling and floor control this; floor clamps like PostgreSQL:

sql
SELECT date('2026-01-31', '+1 month', 'floor') AS floor_rule, date('2026-01-31', '+1 month', 'ceiling') AS ceiling_rule;
floor_ruleceiling_rule
2026-02-282026-03-03

When the rule is "same day next month, or the last day if it does not exist", write it explicitly, or compute from the start of the month.

sql
SELECT date('2026-01-31', '+1 month') AS plus_one_month;

SELECT date('2026-01-31', '+1 month', 'floor') AS floor_rule, date('2026-01-31', '+1 month', 'ceiling') AS ceiling_rule;

Checked on SQLite 3.46.1, run twice with identical output. PostgreSQL results in the answer were checked on PostgreSQL 17.12.

Key points

  • SQLite +1 month normalizes Feb 31 to 3 March
  • PostgreSQL clamps to the last valid day
  • SQLite 3.46 adds floor and ceiling modifiers
  • State the month-end rule explicitly in billing logic

Common mistakes

  • Assuming month arithmetic is portable
  • Generating monthly due dates by repeatedly adding one month to the 31st

Follow-ups

  • How would you produce the last day of each month?
  • How would you generate monthly billing dates from a start date?

Tips

  • Name both results and the reason