Table of contents
1.
Introduction
2.
Syntax, Parameter & Return Type of the DATEADD() function
2.1.
Syntax
2.2.
Parameters
2.3.
Return Type
3.
Examples of DATEADD() Function
3.1.
Adding Days to a Date
3.1.1.
Syntax
3.2.
SQL
3.2.1.
Output
3.3.
Adding Months to a Date
3.4.
SQL
3.4.1.
Output
3.5.
Adding Years to a Date
3.5.1.
Syntax
3.6.
SQL
3.6.1.
Output
3.7.
Adding Quarters to a Date
3.7.1.
Syntax
3.8.
SQL
3.8.1.
Output
3.9.
Subtracting Days from a Date
3.9.1.
Syntax
3.10.
SQL
3.10.1.
Output
3.11.
Subtracting Months from a Date
3.11.1.
Syntax
3.12.
SQL
3.12.1.
Output
3.13.
Subtracting Years from a Date
3.13.1.
Syntax
3.14.
SQL
3.14.1.
Output
3.15.
Adding Hours to a Time
3.15.1.
Syntax
3.16.
SQL
3.16.1.
Output
3.17.
Adding Minutes to a Time
3.17.1.
Syntax
3.18.
SQL
3.18.1.
Output
3.19.
Adding Seconds to a Time
3.19.1.
Syntax
3.20.
SQL
3.20.1.
Output
3.21.
Adding Milliseconds to a Time
3.21.1.
Syntax
3.22.
SQL
3.22.1.
Output
3.23.
Subtracting Hours from a Time
3.23.1.
Syntax
3.24.
SQL
3.24.1.
Output
3.25.
Subtracting Minutes from a Time
3.25.1.
Syntax
3.26.
SQL
3.26.1.
Output
3.27.
Subtracting Seconds from a Time
3.27.1.
Syntax
3.28.
SQL
3.28.1.
Output
4.
Frequently Asked Questions
4.1.
Is it possible to apply arithmetic operations on Dates in SQL?
4.2.
How can we handle time zones in a SQL database?
4.3.
How does DATEADD() function affect the performance of the query?
5.
Conclusion
Last Updated: Mar 27, 2024
Easy

SQL Server DATEADD() Function

Author Aayush Sharma
0 upvote
Career growth poll
Do you think IIT Guwahati certified course can help you in your career?

 

Introduction

In Database Management Systems, Sql is one of the most powerful and useful method platforms for data handling. The date is one of the most commonly used Datatypes in SQL. SQL Server provides plenty of date and time methods to manipulate data present in the form of date and time.

sql server

In this blog, we will discuss the SQL Server DATEADD() function in detail. We will study the required parameters and the return type of the DateADD() function. We will also look at some examples of DATEADD() functions along with their code implementations. In the end, we will conclude by discussing some applications and some frequently asked questions.

Syntax, Parameter & Return Type of the DATEADD() function

In this section, we will discuss the syntax, parameters, and return type of the DATEADD() function in detail.

Syntax

DATEADD (interval, number, date)

The syntax of the DATEADD() function is pretty simple. It takes three parameters, the interval, the number of intervals to be addedand the initial date. All of these parameters are required.

Parameters

The DATEADD function takes three parameters:
 

  • Interval - It is the time or date interval required to be added.
     
  • Number - This is the amount of intervals to be added to the date.
     
  • Date - This is the initial date that will be modified.
     

All of the parameters values along with their notations are described with the help of a table below.

Parameter

Description / Notation

Interval

Year - year, yyyy, yy

 

Quarter - quarter, qq, q

 

Month - month, mm, m

 

Day - day, dd, d

 

Day of the year - dayofyear, dy, y

 

Week - week, ww, wk

 

Weekday - weekday, dw, w

 

Minute - minute, mi, m

 

Second - second, ss, s

 

Hour - hour, hh

 

Millisecond - millisecond, ms

                      Number                                                     

It is the number of intervals to be added to the original date/time data. It can be either positive (future dates) or negative (past dates).
Date This is the initial date/time to be modified.

Return Type

After performing the operations on the initial date, the DATEADD() function returns the modified date in a format similar to the initial date.

Also see, SQL EXCEPT

Examples of DATEADD() Function

In this section, we will explore some examples of the DATEADD() function to have a better understanding of it.

Adding Days to a Date

In this example, we will add ten days to our initial date.

Syntax

  • SQL

SQL

SELECT DATEADD(Day, 10, '2023-07-29') AS ModifiedDate;

Output

2023-08-08 00:00:00.000

Adding Months to a Date

In this example, we will add ten months to our initial date.

Syntax

  • SQL

SQL

SELECT DATEADD(Month, 10, '2023-07-29') AS ModifiedDate;

Output

2024-05-29 00:00:00.000

Adding Years to a Date

In this example, we will add ten years to our initial date.

Syntax

  • SQL

SQL

SELECT DATEADD(Year, 10, '2023-07-29') AS ModifiedDate;

Output

2033-07-29 00:00:00.000

Adding Quarters to a Date

In this example, we will add ten quarters to our initial date.

Syntax

  • SQL

SQL

SELECT DATEADD(Quarter, 10, '2023-07-29') AS ModifiedDate;

Output

2026-01-29 00:00:00.000

Subtracting Days from a Date

In this example, we will subtract ten days from our initial date.

Syntax

  • SQL

SQL

SELECT DATEADD(Day, -10, '2023-07-29') AS ModifiedDate;

Output

2023-07-19 00:00:00.000

Subtracting Months from a Date

In this example, we will subtract ten months from our initial date.

Syntax

  • SQL

SQL

SELECT DATEADD(Month, -10, '2023-07-29') AS ModifiedDate;

Output

2022-09-29 00:00:00.000

Subtracting Years from a Date

In this example, we will subtract ten years from our initial date.

Syntax

  • SQL

SQL

SELECT DATEADD(Year, -10, '2023-07-29') AS ModifiedDate;

Output

2013-07-29 00:00:00.000

Adding Hours to a Time

In this example, we will add ten hours to our initial date.

Syntax

  • SQL

SQL

SELECT DATEADD(Hour, 10, '2023-07-29') AS ModifiedDate;

Output

2023-07-29 10:00:00.000

Adding Minutes to a Time

In this example, we will add ten minutes to our initial date.

Syntax

  • SQL

SQL

SELECT DATEADD(Minute, 10, '2023-07-29') AS ModifiedDate;

Output

2023-07-29 00:10:00.000

Adding Seconds to a Time

In this example, we will add ten seconds to our initial date.

Syntax

  • SQL

SQL

SELECT DATEADD(Second, 10, '2023-07-29') AS ModifiedDate;

Output

2023-07-29 00:00:10.000

Adding Milliseconds to a Time

In this example, we will add ten milliseconds to our initial date.

Syntax

  • SQL

SQL

SELECT DATEADD(Millisecond, 10, '2023-07-29') AS ModifiedDate;

Output

2023-07-29 00:00:00.010

Subtracting Hours from a Time

In this example, we will add ten hours from our initial date.

Syntax

  • SQL

SQL

SELECT DATEADD(Hour, -10, '2023-07-29') AS ModifiedDate;

Output

2023-07-28 14:00:00.000

Subtracting Minutes from a Time

In this example, we will add ten minutes from our initial date.

Syntax

  • SQL

SQL

SELECT DATEADD(Minute, -10, '2023-07-29') AS ModifiedDate;

Output

2023-07-28 23:50:00.000

Subtracting Seconds from a Time

In this example, we will add ten seconds from our initial date.

Syntax

  • SQL

SQL

SELECT DATEADD(Second, -10, '2023-07-29') AS ModifiedDate;

Output

2023-07-28 23:59:50.000

Frequently Asked Questions

Is it possible to apply arithmetic operations on Dates in SQL?

Yes, it is possible to perform arithmetic operations on SQL. These operations include addition, subtraction, time intervals between dates, etc. These operations are supported by functions like DATEADD() and DATEDIFF() in the SQL.

How can we handle time zones in a SQL database?

Generally, we should store the time and dates in some standardized time zone (UTC, GMT, IST, etc.). If we want the date and time in some other time zone, we can convert them into your required time zone by using SQL functions like CONVERT().

How does DATEADD() function affect the performance of the query?

The effect of the DATEADD() function depends on the size of the dataset and the complexity of the query you are using. If the dataset is big or if the DATEADD() query is being used too much, it can affect the performance of the query making it slow and inefficient.

Conclusion

In this article, we discussed SQL Server DATEADD() function. We discussed the parameters and return type of the DATEADD() function. We also discussed some common examples of the DATEADD() function along with their code implementations. In the end, we concluded by discussing some key applications of the DATEADD() function and some frequently asked questions.

So now that you know about the SQL Server DATEADD() function, you can refer to similar articles.
 

You may refer to our Guided Path on Code Studios for enhancing your skill set on DSA, Competitive Programming, System Design, etc. Check out essential interview questions, practice our available mock tests, look at the interview bundle for interview preparations, and so much more!

Happy Learning!

Live masterclass