Add and Subtract Dates using DATEADD in SQL Server

Problem

Date manipulation is a common scenario when retrieving or storing data in a Microsoft SQL Server database. There are several date functions (DATENAME, DATEPART, DATEADD, DATEDIFF, etc.) that are available and in this tutorial, we look at how to use the DATEADD function in SQL queries, stored procedures, T-SQL scripts, etc. for OLTP databases as well as data warehouse and data science projects.

Solution

The DATEADD function simply allows you to add or subtract the specified number of units of time to a specified date/time value.

SQL Server DATEADD Function

The T-SQL syntax of the DATEADD function is as follows:

DATEADD(<Unit of time>, <Units>, <Input Date>)
--
Syntax to add
5 days to September 1, 2011 (input date) the function would be
DATEADD(DAY, 5, '9/1/2011')
-- Syntax to subtract 5 months from September 1, 2011 (input date) the function would be
DATEADD(MONTH, -5, '9/1/2011')

Using DATEADD Function and Examples

  1. Add 30 days to a date SELECT DATEADD(DD,30,@Date)
  2. Add 3 hours to a date SELECT DATEADD(HOUR,3,@Date)
  3. Subtract 90 minutes from date SELECT DATEADD(MINUTE,-90,@Date)
  4. Check out the chart to get a list of all options

Date Formats and Units of Time

A thing to note is that the date format can be any date format that SQL Server recognizes such as:

  • 9/1/2011
  • 9/1/2011 12:30
  • 9/1/2011 12:30:999
  • 2011-09-01
  • 2011-09-01 12:30
  • etc…

Here are the units of time, the SQL Server versions in which they can be used, and abbreviations that can be used instead of the full unit of time name:

Units of Time Available in SQL Server
Unit of time2000/20052008 +Abbreviations
NANOSECONDNoYesns
MICROSECONDNoYesmcs
MILLISECONDYesYesms
SECONDYesYesss, s
MINUTEYesYesmi, n
HOURYesYeshh
WEEKDAYYesYesdw, w
WEEKYesYeswk, ww
DAYYesYesdd, d
DAYOFYEARYesYesdy, y
MONTHYesYesmm, m
QUARTERYesYesqq, q
YEARYesYesyy, yyyy

The table above reveals that there are some units of time that cannot be used with earlier versions of SQL Server. SQL Server 2008 and later introduced new date/time data types: DATETIME2, TIME, and DATETIMEOFFSET. The MICROSECOND and NANSECOND units of time were introduced as well, but cannot be used in earlier versions of SQL Server. Another thing to keep in mind is that you can’t use the seconds unit of time with the DATE data type.

SQL Server DATEADD Function Examples

For all of these Transact-SQL examples, the parameter @Date = “2011-09-23 15:48:39.2370000”, which we consider our current date.  We can test the SQL commands as follows:

DECLARE @Date datetime2 = '2011-09-23 15:48:39.2370000'
SELECT DATEADD(NANOSECOND,150000,@Date)
Examples of Using the DATEADD Function
Unit of timeQueryResult
NANOSECONDSELECT DATEADD(NANOSECOND,150000,@Date)2011-09-23 15:48:39.2371500
MICROSECONDSELECT DATEADD(MICROSECOND,150000,@Date)2011-09-23 15:48:39.3870000
MILLISECONDSELECT DATEADD(MILLISECOND,1500,@Date)2011-09-23 15:48:40.7370000
SECONDSELECT DATEADD(SECOND,1500,@Date)2011-09-23 16:13:39.2370000
MINUTESELECT DATEADD(MINUTE,15,@Date)2011-09-23 16:03:39.2370000
HOURSELECT DATEADD(HOUR,-3,@Date)2011-09-23 12:48:39.2370000
WEEKDAYSELECT DATEADD(WEEKDAY,-2,@Date)2011-09-21 15:48:39.2370000
WEEKSELECT DATEADD(WEEK,4,@Date)2011-10-21 15:48:39.2370000
DAYSELECT DATEADD(DD,4,@Date)2011-09-27 15:48:39.2370000
DAYOFYEARSELECT DATEADD(DY,14,@Date)2011-10-07 15:48:39.2370000
MONTHSELECT DATEADD(MM,-4,@Date)2011-05-23 15:48:39.2370000
QUARTERSELECT DATEADD(Q,-2,@Date)2011-03-23 15:48:39.2370000
YEARSELECT DATEADD(YYYY,-5,@Date)2006-09-23 15:48:39.2370000

Example 1

A practical use of DATEADD is to return a user-friendly length of time that has elapsed between two times, like how long it takes for a student to complete an exam.

Below we are also using the DATEDIFF function to find the difference between the start and end time in seconds, then adding the number of seconds to 01/01/1900, which can be represented as a 0.

DECLARE @StartTime DATETIME = '2011-09-23 15:00:00'
       ,@EndTime   DATETIME = '2011-09-23 17:54:02'
       
SELECT CONVERT(VARCHAR(8), DATEADD(SECOND, DATEDIFF(SECOND,@StartTime, @EndTime),0), 108) as ElapsedTime
-- the output would be 02:54:02

Example 2

Another use would be when retrieving data based on a period.  The procedure could pass in a start date and a number of days to retrieve data.

DECLARE @StartDate DATETIME = '2005-07-01'
DECLARE @Units INT = 7
SELECT * 
FROM AdventureWorks2008R2.Production.WorkOrder
WHERE StartDate BETWEEN @StartDate AND DATEADD(DAY, @Units, @StartDate)

Next Steps

9 Comments

  1. Great overview of DATEADD! It’s one of those deceptively simple functions that ends up being a lifesaver in real-world scenarios, whether it’s shifting reporting periods, managing ETL logic, or handling time-based analytics.

  2. Nice and exhaustive post, thank you!

    PS: please fix a typo in item #2 of “Using DATEADD Function and Examples” paragraph

  3. Thank you that solve my problem. I test it with 5 , 30, 65, 365 and 730 days and it give me the correct date WITH NO ERRORS.

    Thanks so much.
    The next question will come soon..

  4. Hi Arrie,

    can you just pass the interval value for each record, instead of hard coding the value.

    SELECT DATE_ADD(base_date, INTERVAL frequency DAY);

    -Greg

  5. Hi Greg
    This article will not solve my problem.
    ID base_date frequency next_insp
    1 2021-07-22 90 ??
    2 2021-05-15 180 ??
    3 2022-01-03 365 ??
    4 2022-04-25 70 ??

    As you can see is that all the frequency days are not the same. I cant use “INTERVAL” because Interval is a fix amount of days.. When you add say 25 days to all the dates.

    Test it self
    Arrie

  6. Hi Arrie,

    This article is for how to do this in SQL Server.

    Check out this one for MySQL – https://www.w3schools.com/SQl/func_mysql_date_add.asp

    -Greg

  7. Can you solve this please. ID Primary key, table asset_type VARCHAR(30), Today_date DATE, base_date DATE, frequency INT, next_insp DATE.
    in the DEFAULT i need to add frequency (days) to the base_date. (‘base_date’ + ‘frequency’).
    The frequency for every row is different, and can be anything from 30(days) to 730 ( 2 years)
    As I enter days within the month say 2022-03-10 + 20 I get 2022-03-30 witch is correct. But if I enter a number like 65 (days) there is an error and in the error it display 20220370.
    I tried ADDDATE, DATE_ADD but nothing work.
    I need to store this next_insp date.

    I hope you can help. I am using MySQL Workbench. I am new to MySQL if you can keep it simple for me to understand please.
    Thank you so much

  8. The only display format allowed for dates in the ANSI ISO standards for SQL is based on ISO - 8601. It is “yyyy-mm-dd”, with the dashes we made this decision so that the display be unambiguous and could always be parsed correctly.

Leave a Reply

Your email address will not be published. Required fields are marked *