Skip to main content
Database Administration & Troubleshooting

SQL Server DATEADD CLI Reference: Syntax & Troubleshooting

sql server dateadd is the T-SQL function in Microsoft SQL Server that adds or subtracts a specified number of time units to a date or datetime value, enabling calendar arithmetic and deadline calculations. It is part of the built-in date/time functions and supports all dateparts from nanoseconds to years.

-- Basic syntax as per Microsoft Learn
DATEADD ( datepart , number , date )

Tested on SQL Server 2022 (16.x) with standard T-SQL compatibility.

The function accepts three mandatory arguments:

  • datepart – the unit of time to add (e.g., year, month, day, hour, minute, second, nanosecond).
  • number – an integer (or bigint) value specifying the interval to add. Negative values subtract.
  • date – the base date expression; can be a column, literal, variable, or expression that resolves to a date/time type.

sql server dateadd Rapid Reference Cheat Sheet

Action SQL Command Database Context Key Datepart Result
Add 1 year DATEADD(year, 1, '2024-01-01') SQL Server / Azure SQL year 2025-01-01
Subtract 3 months DATEADD(month, -3, '2024-06-15') SQL Server / Azure SQL month 2024-03-15
Add 14 days DATEADD(day, 14, '2024-12-31') SQL Server / Azure SQL day 2025-01-14
Equivalent in MySQL DATE_ADD('2024-01-01', INTERVAL 1 YEAR) MySQL 8.x INTERVAL unit 2025-01-01
Equivalent in PostgreSQL '2024-01-01'::date + INTERVAL '1 year' PostgreSQL 16 INTERVAL literal 2025-01-01
Add 2 hours DATEADD(hour, 2, '2024-10-10 10:30:00') SQL Server / Azure SQL hour 2024-10-10 12:30:00
See also  install mysql on ubuntu: CLI Reference & Troubleshooting

Advanced Implementation & Parameters

Datepart argument

SQL Server supports all valid datepart abbreviations and full names. The full list from Microsoft documentation:

datepart Abbreviations
year yy, yyyy
quarter qq, q
month mm, m
dayofyear dy, y
day dd, d
week wk, ww
weekday dw, w
hour hh
minute mi, n
second ss, s
millisecond ms
microsecond mcs
nanosecond ns

Note: Using weekday adds the same number of days as day; it does not adjust to business days.

Number argument behavior

The number parameter accepts data types int and bigint. Fractional values are truncated (not rounded). For very large numbers (e.g., adding 1,000,000 days), DATEADD may return NULL or raise Msg 517 (adding to a smalldatetime beyond its range) or Msg 8115 (arithmetic overflow). Using bigint for the number parameter helps avoid overflow in intermediate calculations.

Date argument and return types

DATEADD returns the same data type as the date argument, unless the input is a string literal—then the output is datetime. For smalldatetime inputs, any datepart finer than minute (i.e., second, millisecond) is ignored and returns smalldatetime with seconds set to 00.

-- smalldatetime truncates seconds
SELECT DATEADD(second, 30, CAST('2024-01-01 12:00:00' AS smalldatetime));
-- Output: 2024-01-01 12:00:00 (no change)

Examples from official documentation

-- A. Increment datepart by an interval of 1
SELECT DATEADD(year, 1, '2024-01-01');

-- B. Increment more than one level in one statement
SELECT DATEADD(month, 18, '2024-01-01');   -- adds 1 year + 6 months

-- C. Use expressions as arguments
DECLARE @days int = 10;
SELECT DATEADD(day, @days, GETDATE());

-- Specify a column as date
SELECT EmployeeID, DATEADD(day, -30, HireDate) AS ProbationEnd
FROM Employees;

Error Resolution & Troubleshooting

Error Code / Signal Root Cause Remediation Command / Approach
Msg 517, Level 16, State 1 Adding seconds/milliseconds to a smalldatetime crosses the date range (1900-01-01 to 2079-06-06). Cast to datetime or datetime2 before DATEADD:

SELECT DATEADD(second, 1, CAST('2079-06-06' AS datetime));
Msg 8115, Level 16, State 2 Arithmetic overflow converting expression to data type datetime. Caused by adding a very large number (e.g., 10,000,000 days) or a number that pushes the date beyond 9999-12-31. Use datetime2 which supports a wider range (0001-01-01 through 9999-12-31) and higher precision:

SELECT DATEADD(day, 1000000, CAST('2024-01-01' AS datetime2));
Invalid datepart given (e.g., ‘decade’) Non-standard datepart name or abbreviation. Always use one of the 13 allowed dateparts. Check sys.dateparts or official documentation.
NULL result or unexpected date shift Using weekday instead of day; or number is NULL; or date is NULL. Use ISNULL to handle nulls:

SELECT DATEADD(day, ISNULL(@days, 0), GETDATE());
Fractional seconds precision loss Using smalldatetime or datetime where millisecond rounding changes the result. Switch to datetime2(3) or datetime2(7) for millisecond/microsecond accuracy.
See also  NTILE in SQL: Syntax, Examples, and Troubleshooting

Production-Grade Implementation

  • Always use four-digit years to avoid ambiguity with the two-digit year cutoff server configuration (SELECT SERVERPROPERTY('TwoDigitYearCutoff')).
  • Prefer datetime2 over datetime or smalldatetime for new development. datetime2 offers a larger range (0001-01-01 to 9999-12-31) and higher fractional second precision (7 digits), reducing overflow and truncation.
  • Use bigint for the number parameter when adding extremely large intervals or when the number may exceed int range (2,147,483,647).
  • Cache static date calculations in computed columns or indexed views when possible, instead of recomputing DATEADD in every query.
  • Test edge cases around month boundaries and leap years. For example, DATEADD(month, 1, '2024-01-31') returns 2024-02-29 (last day of February), not 2024-02-28.
  • Avoid using DATEADD directly in WHERE clauses on large tables without sargability. Instead, compute the boundary date first and filter against the column.

Frequently Asked Questions

What is the difference between DATEADD and DATEDIFF in SQL Server?

Answer: DATEADD adds an interval to a date; DATEDIFF returns the count of intervals between two dates.

DATEADD(datepart, number, date) returns a new date by adding a specified interval. DATEDIFF(datepart, startdate, enddate) returns an integer. Example:

SELECT DATEADD(day, 7, GETDATE()) AS OneWeekLater;
SELECT DATEDIFF(day, '2025-01-01', '2025-12-31') AS DaysInYear;

When should I use the month datepart with DATEADD in SQL Server?

Answer: Use month to advance or decrement dates by calendar months, e.

DATEADD(month, n, date) handles month-end boundary logic automatically. For example, adding 1 month to January 31 returns February 28 (or 29 in leap year).

-- Get first day of next month
SELECT DATEADD(month, DATEDIFF(month, 0, GETDATE()) + 1, 0) AS NextMonthStart;

How do I fix ‘Arithmetic overflow error converting expression to data type int’ when using DATEADD with large intervals?

Answer: Cast the date column to DATETIME2 or DATE before applying DATEADD, or reduce the interval value to avoid int overflow.

See also  MySQL Users CLI Reference: Syntax & Troubleshooting Guide

The default DATETIME range is 1753-9999; adding a high number of seconds can overflow the internal integer arithmetic. Use DATETIME2 (range 0001-9999):

SELECT DATEADD(second, 1000000000, CAST(GETDATE() AS DATETIME2));
-- Or use DATEADD(hour, n, date) for larger intervals implicitly

Does DATEADD work on Amazon RDS for SQL Server?

Answer: Yes, DATEADD is fully supported on Amazon RDS for SQL Server as part of T-SQL, identical to on-premises behavior.

RDS for SQL Server runs Enterprise and Standard editions with full engine compatibility. All valid datepart values (day, month, year, etc.) and negative numbers work natively. No special configuration required.

-- Example on RDS
SELECT DATEADD(day, -30, GETDATE()) AS ThirtyDaysAgo;

What is the fastest way to get the first day of the current month using DATEADD?

Answer: Use DATEADD(month, DATEDIFF(month, 0, GETDATE()), 0).

This method leverages the zero-date anchor (1900-01-01). For performance-critical queries (e.g., millions of rows), it reduces CPU overhead compared to DATEPART or DAY functions.

SELECT DATEADD(month, DATEDIFF(month, 0, GETDATE()), 0) AS FirstOfMonth;
-- Equivalent but slightly slower:
SELECT DATEADD(day, 1-DAY(GETDATE()), CAST(GETDATE() AS DATE));