SQL SERVER: Dateadd function - Free Asp.Net,MVC,AngularJs,Jquery,Javascript,Sql Server,WCF,Entity framework snippets and tutorial

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


What do you think about this article?

If you found this article useful, please share and follow on Facebook, Twitter, Google Plus and other social media websites. To get free updates subscribe to newsletter. Please put your thoughts and feedback in comments section.

Share this

Share on FacebookTweet on TwitterPlus on Google+


EmoticonEmoticon