DateAdd SQL Server: Add or Subtract Dates in SQL Server

Hello Dev, are you looking for a way to manipulate dates in SQL Server? If so, you’re in the right place! In this article, we’ll be discussing the DateAdd function in SQL Server and how it can be used to add or subtract dates. Let’s dive right in!

What is DateAdd?

The DateAdd function is a built-in function in SQL Server that can be used to add or subtract a specified number of date or time intervals to a date. It is particularly useful when you need to perform date calculations or create date-based queries.

Interval
Abbreviation
Description
year
YY, YYYY
Adds or subtracts the specified number of years to a date.
quarter
QQ, Q
Adds or subtracts the specified number of quarters to a date.
month
MM, M
Adds or subtracts the specified number of months to a date.
dayofyear
DY, Y
Adds or subtracts the specified number of days to the date, counting from the first day of the year.
day
DD, D
Adds or subtracts the specified number of days to a date.
week
WK, WW
Adds or subtracts the specified number of weeks to a date.
hour
HH
Adds or subtracts the specified number of hours to a time.
minute
MI, N
Adds or subtracts the specified number of minutes to a time.
second
SS, S
Adds or subtracts the specified number of seconds to a time.
millisecond
MS
Adds or subtracts the specified number of milliseconds to a time.
microsecond
MCS
Adds or subtracts the specified number of microseconds to a time.
nanosecond
NS
Adds or subtracts the specified number of nanoseconds to a time.

Using DateAdd to Add Dates in SQL Server

Let’s say you have a table with a column called “OrderDate” and you want to add 30 days to every order. Here’s how you can do it using the DateAdd function:

UPDATE Orders SET OrderDate = DATEADD(day, 30, OrderDate)

This statement adds 30 days to each OrderDate value in the Orders table. The first argument, “day”, specifies the interval to add. The second argument, 30, specifies the number of days to add. The third argument, OrderDate, specifies the date to which the interval should be added.

You can also use DateAdd to subtract dates. For example, if you want to subtract 2 weeks from a given date:

SELECT DATEADD(week, -2, '2021-01-01')

This statement subtracts 2 weeks from the date ‘2021-01-01’. The first argument, “week”, specifies the interval to subtract. The second argument, -2, specifies the number of weeks to subtract. The third argument, ‘2021-01-01’, specifies the date from which the interval should be subtracted.

Using DateAdd with Variables and Functions

DateAdd can also be used with variables and functions. For example, you can use the DateAdd function with the GetDate() function to add a certain number of days to the current date:

SELECT DATEADD(day, 7, GETDATE())

This statement adds 7 days to the current date and returns the result.

READ ALSO  Linux Hosting Virtual Server: A Complete Guide for Dev

You can also use DateAdd with variable dates. For example, if you have a variable called @StartDate and you want to add 3 months to it:

SET @StartDate = '2021-01-01'SELECT DATEADD(month, 3, @StartDate)

This statement adds 3 months to the @StartDate variable and returns the result.

FAQs

What is the syntax of DateAdd function in SQL Server?

The syntax of the DateAdd function in SQL Server is:

DATEADD(interval, number, date)

The “interval” parameter specifies the interval to add or subtract, such as year, month, day, etc. The “number” parameter specifies the number of intervals to add or subtract. The “date” parameter specifies the date to which the intervals should be added or subtracted.

Can I use DateAdd to add or subtract milliseconds?

Yes, you can use DateAdd to add or subtract milliseconds. Simply use the “millisecond” interval and specify the number of milliseconds to add or subtract.

Can I use DateAdd to subtract dates?

Yes, you can use DateAdd to subtract dates. Simply use a negative number for the “number” parameter to subtract the specified interval.

Can I use DateAdd with datetime values?

Yes, DateAdd can be used with both date and time values. Simply specify the appropriate interval for the date or time portion.

Can I use DateAdd with NULL values?

No, you cannot use DateAdd with NULL values. If any of the parameters are NULL, the result will be NULL.

Is there a limit to the number of intervals I can add or subtract?

There is no limit to the number of intervals you can add or subtract using DateAdd. However, keep in mind that adding or subtracting a large number of intervals may result in unexpected or inaccurate results.