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
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
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
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
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
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
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
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
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
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
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
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
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
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
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!