Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 Add a number of months to date

Author  Topic 

wembleybear
Yak Posting Veteran

93 Posts

Posted - 2014-10-15 : 08:33:48
I have a simple query which returns a purchase date plus the number of months warranty an item has. What I need to do is add these months to the purchase date to get a warranty expiry date. What is the best way to do this? My query and results are below, I need an extra column in the results with that calculated warranty date:

select item, purchase_date, warranty_period 
from inventory where item like '05MC%'

item purchase_date warranty_period
-------------------- ----------------------- ---------------
05MC0001 2014-07-22 00:00:00 36
05MC0002 2014-07-11 00:00:00 36


Many thanks for your help
Martyn

James K
Master Smack Fu Yak Hacker

3873 Posts

Posted - 2014-10-15 : 08:36:11
Use the DATEADD function.
DATEADD(mm,warranty_period,purchase_date) as Warranty_Expiry_Date
You could even make it a computed column.
http://msdn.microsoft.com/en-us/library/ms186819.aspx
http://technet.microsoft.com/en-us/library/ms191250(v=sql.105).aspx
Go to Top of Page

gbritton
Master Smack Fu Yak Hacker

2780 Posts

Posted - 2014-10-15 : 08:36:42
select item, purchase_date, warranty_period, dateadd(month, warranty_period, purchase_date) as warranty_expiry_date
Go to Top of Page

wembleybear
Yak Posting Veteran

93 Posts

Posted - 2014-10-15 : 08:44:31
Worked perfectly - thanks guys!

Martyn
Go to Top of Page
   

- Advertisement -