Add One or More Days to Date in SQL Server

Day can be added to date or datetime in sql server using the following function.

Function :

DATEADD( datepart, number, date )

Arguments :

datepart : It is the part of date ( ex : day - for adding days to the date specified in 3rd argument )

number : It is an integer which is added to the datepart ( ex : day ) of date

date : date , to which we want to add number of days

Examples  :

1. Add 2 days to date : 2014-09-24

SELECT DATEADD(day, 2, '2014-09-24') as '( 2014-09-24 ) + 2 days';
                         (or)
SELECT DATEADD(dd, 2, '2014-09-24') as '( 2014-09-24 ) + 2 days';
                         (or)
SELECT DATEADD(d, 2, '2014-09-24') as '( 2014-09-24 ) + 2 days';
( 2014-09-24 ) + 2 days
----------------------------
2014-09-26 00:00:00.000

 

2. Getting today and tomorrow ( today + 1 day )

SELECT GETDATE() as 'Today', DATEADD(day, 1, GETDATE()) as 'Tomorrow';
                          (or)
SELECT GETDATE() as 'Today', DATEADD(dd, 1, GETDATE()) as 'Tomorrow';
                          (or)
SELECT GETDATE() as 'Today', DATEADD(d, 1, GETDATE()) as 'Tomorrow';
Today Tomorrow
----------------------------------------------------
2014-09-25 23:56:43.757 2014-09-26 23:56:43.757

References :

http://msdn.microsoft.com/en-us/library/ms186819.aspx

 

 

Submit a Comment

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

www.000webhost.com