DATEADD(): Functions is used when we want to get date by
adding days or month or year in given date. It will display the date and
time by adding or subtracting date and time interval.
Syntax
DATEADD(DATEPART, number,
datecolumn)
|
You
need to mention datepart here i.e. Days, month or year.
For
ex.
Query to get Date & time
after 5 days from current datetime in MS SQL:
Select DATEADD(DAY, 5, GETDATE()) as
NewDatetime
|
Query to get Date & time 5
days ago from current datetime in MS SQL:
Select DATEADD(DAY, -5, GETDATE()) as
NewDatetime
To get previous date, we need
to subtract datepart i.e. Day, Month or Year.
|
Add 5 Months to Current Date:
Select DATEADD(MONTH, 5, GETDATE()) as
NewDatetime
|
Add 5 years to Current Date:
Select DATEADD(YEAR, 5, GETDATE()) as
NewDatetime
|