Wednesday, March 29, 2017

SQL SERVER: Dateadd function

In this article I am going to explain Dateadd function of Sql server.


Description:
Dateadd function of sql server is used to add or subtract date and time.
Syntax:
dateadd(date/timeinterval,number,date)

Unit of date
Info
Day,dd,d,dy,y
Day
Month,mm,m
Month
Year,yy,yyyy
Year
Quarter,qq,q
Quarter
Week,wk,ww
Week

Unit of time
Info
Hour,hh
Hour
Minute,mi,n
Minute
Second,ss,s
Second
Millisecond,ms
Millisecond

Implementation:
Example add day:
declare @date date = getdate()
declare @dateadd date = dateadd(day,7,getdate())
select @dateadd as 'day added'

Output:
day added
2017-04-05

Example subtract  day: 
declare @date datetime = getdate()
declare @dateadd datetime = dateadd(dd,-7,getdate())
select @dateadd as 'day Subtract'

Output:
Day subtract
2017-03-22 12:41:40.553


No comments:

Post a Comment