Link to home
Start Free TrialLog in
Avatar of RustyZ32
RustyZ32Flag for United States of America

asked on

Setting a datetime variable to current date - 7 days

I know how to declare and set a variable to get the current time minus a specified number of hours as shown below:

Declare @time datetime
set @time = (select getdate ())- '04:00:00'


but I cannot figure out how to do the same but with days instead of just hours.

Avatar of RiteshShah
RiteshShah
Flag of India image

declare @time datetime

select @time=dateadd(dd,-2,getdate())

Avatar of Patrick Matthews
Declare @time datetime
set @time = DATEADD(hour, -4, getdate ())
Sorry, misread the question :)
ASKER CERTIFIED SOLUTION
Avatar of RiteshShah
RiteshShah
Flag of India image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
with the help of DateAdd function, you can add/subtract hour, minutes, days, months etc. for more details about DateAdd function.

http://msdn.microsoft.com/en-us/library/ms186819.aspx
If you are trying to set the date back 7 days AND 4 hours, you can nest the dateadd function.  

declare @time datetime
select @time=dateadd(dd,-7,(dateadd(hh,-4,getdate())))
select @time