Thursday, March 22, 2012

Adding two dates

I have two fields, Date Purchased (smalldatetime) and warranty in months (integer).

can anyone help me to formulate a query to show the date when the warranty ends, and/or the remaining days/months in the warranty please?

I'm not sure if its doable, but i would really appreciate it if anyone can help me!

Give a look to the DATEADD function in books online; it looks like this:

declare @.example table
( rid integer,
datePurchased smalldatetime,
warranty integer
)

insert into @.example
select 1, '4/8/7', 3 union all
select 2, '5/14/7', 12

select rid,
convert(varchar(10), datePurchased, 101) as datePurchased,
warranty,
dateadd (mm, warranty, datePurchased) as warrantyEndDate,
datediff (day, getdate(), dateadd (mm, warranty, datePurchased)) as daysRemaining,
datediff (mm, getdate(), dateadd (mm, warranty, datePurchased)) as monthsRemaining
from @.example

/*
rid datePurchased warranty warrantyEndDate daysRemaining monthsRemaining
-- - -- - -
1 04/08/2007 3 2007-07-08 00:00:00 45 2
2 05/14/2007 12 2008-05-14 00:00:00 356 12
*/

No comments:

Post a Comment