RustyZ32
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.
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.
Declare @time datetime
set @time = DATEADD(hour, -4, getdate ())
set @time = DATEADD(hour, -4, getdate ())
Sorry, misread the question :)
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
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
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,(datea dd(hh,-4,g etdate())) )
select @time
declare @time datetime
select @time=dateadd(dd,-7,(datea
select @time
select @time=dateadd(dd,-2,getdat