T-SQL reference › DATEADD
DATEADD in SQL Server: add or subtract days, weeks, months
DATEADD moves a date by a number of units. Negative numbers go back in time. It is how almost every “last N days” filter is written.
Syntax
DATEADD(datepart, number, date)
The date part is written as a word, not a string: day, week, month, year, hour, minute. Short forms like dd and mm also work but are harder to read.
Examples
SELECT DATEADD(day, -30, CAST(GETDATE() AS date)) AS thirty_days_ago,
DATEADD(week, -1, CAST(GETDATE() AS date)) AS a_week_ago,
DATEADD(month, 1, '2026-01-15') AS next_month,
DATEADD(year, -1, CAST(GETDATE() AS date)) AS a_year_ago;
Orders from the last 30 days
SELECT order_id, order_date, total_amount
FROM Orders
WHERE order_date >= DATEADD(day, -30, CAST(GETDATE() AS date))
AND order_date < CAST(GETDATE() AS date)
ORDER BY total_amount DESC;
The first day of last month
SELECT DATEADD(month, -1, DATEFROMPARTS(YEAR(GETDATE()), MONTH(GETDATE()), 1)) AS start_of_last_month;
Watch the end of the month
DATEADD(month, 1, '2026-01-31') returns 2026-02-28. SQL Server clamps to the last day of the shorter month instead of overflowing. Adding a month back again won’t take you to the 31st.
DATEADD keeps the type you give it: pass a date and you get a date, pass GETDATE() and you keep the time part.
Try it on Harbour & Co
James Rowe (Sales Manager) needs this now: “Which orders over £100 came in over the last 30 days? Not today. Biggest first.” Write the query, run it, send the results. Free.
Take the ticket →