MSSQL - How to add to a date or time in MSSQL

In SQL Server, you can use the DATEADD function to add a specified time interval to a given date. The syntax for the DATEADD function is as follows:

DATEADD(datepart, number, date)

The three parameters are:

  • datepart is the part of the date you want to add the interval to (e.g. day, month, year)
  • number is the number of intervals to add
  • date is the date you want to add the interval to

For example, to add three months to the current date:

SELECT DATEADD(month, 3, GETDATE())

To add 30 days to a specific date, you can use the following:

DECLARE @date DATE = '2022-01-01'
SELECT DATEADD(day, 30, @date)

You can use different date parts like 'year', 'month', 'day', 'hour', 'minute', 'second', 'millisecond' to add to those parts of the date.

You can also use DATEADD in a update statement in order to add days to a column in a table, like below:

UPDATE myTable
SET myColumn = DATEADD(day, 30, myColumn)

That is all

I hope you found this helpful, please leave a comment down below with your thoughts!