Note Interval units: YEAR, MONTH, DAY, HOUR, MINUTE, SECOND. Adding months is tricky - Jan 31 + 1 month may yield Feb 28 or Mar 3 depending on the database. PostgreSQL truncates to end of month; MySQL may overflow.
Note PostgreSQL subtracts dates directly. MySQL uses DATEDIFF(end, start) - note the argument order is end first. SQL Server's DATEDIFF takes a unit argument: DATEDIFF(DAY, start, end). Always check argument order in your database.
Frequently asked questions
How does SQL handle subtract date?
SQL covers this with 2 copy-ready snippets on this page. The "Date Addition / Subtraction" snippet in SQL uses `-- PostgreSQL`.
Which statement does the SQL example use?
The "Date Addition / Subtraction" snippet uses `-- PostgreSQL`, from the Date Functions section of the SQL cheat sheet.
What other SQL snippets are shown for "subtract date"?
Besides "Date Addition / Subtraction", this page also shows "Date Difference".
Is there anything to watch out for?
Yes. For "Date Addition / Subtraction": Interval units: YEAR, MONTH, DAY, HOUR, MINUTE, SECOND. Adding months is tricky - Jan 31 + 1 month may yield Feb 28 or Mar 3 depending on the database. PostgreSQL truncates to end of month; MySQL may overflow.