Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Adding Days to Date

Status
Not open for further replies.

charbrad

Technical User
Nov 7, 2002
132
US
Is it possible to add days to a date? For example I have a date column with a date of 10/09/2007 then I have a column named date diff with 365. When adding 365 to 10/09/2007 it should give me 10/09/2008 in the final date column. I am not sure how to do this since the date and the value in the date diff column are not constant. Any suggestions are greatly appreciated.
 
look up dateadd in BOL. Also since 2008 is a leap year, adding 365 to an October 2007 date will not give you the same date in 2008.

"NOTHING is more important in a database than integrity." ESquared
 
You can add dates and numbers together with the + operator. This will ONLY work if you are adding days. If you want to add years, months, hours, minutes, seconds, etc... you'll need to use the DateAdd function.

Ex:

Code:
Create Table #Temp(DateCol DateTime, DateDiffCol Int)

Insert Into #Temp Values('20071009', 365)

Select DateCol + DateDiffCol, DateAdd(day, DateDiffCol, DateCol)
From   #Temp

Drop Table #Temp

BTW, this return 8-oct-2008 because 2008 is a leap year. [wink].

-George

"The great things about standards is that there are so many to choose from." - Fortune Cookie Wisdom
 
Sorry Sis... I didn't see your post when I started writing my own. [blush]

-George

"The great things about standards is that there are so many to choose from." - Fortune Cookie Wisdom
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top