How to Use DATEADD and DATEDIFF in SQL Server

Last updated July 25, 2026 · By the SaturnSQL team

DATEADD(part, n, date) shifts a date by n units; DATEDIFF(part, start, end) counts part boundaries crossed between two dates. Parts include year, quarter, month, week, day, hour, minute, and second.

SELECT
  DATEADD(day, -30, CAST(GETDATE() AS date)) AS thirty_days_ago,
  DATEADD(month, 1, '2026-01-31') AS one_month_later; -- 2026-02-28, clamped

Filter to the last 30 days

SELECT COUNT(*)
FROM events
WHERE created_at >= DATEADD(day, -30, GETDATE());

DATEDIFF counts boundaries, not full periods

DATEDIFF(year, '2025-12-31', '2026-01-01') returns 1 even though only one day passed, because one year boundary was crossed.

SELECT id,
       DATEDIFF(day, created_at, shipped_at) AS days_to_ship
FROM orders
WHERE shipped_at IS NOT NULL;

Run this in SaturnSQL

SaturnSQL is a browser-based SQL editor for teams: shared query library, schema-aware autocomplete, and scheduled exports to Google Sheets and Slack.

Try it free

Related SQL Server guides

© 2026 Panda Capital Oy Ab. All rights reserved.