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.

LVL 6
RustyZ32Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

RiteshShahCommented:
declare @time datetime

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

0
Patrick MatthewsCommented:
Declare @time datetime
set @time = DATEADD(hour, -4, getdate ())
0
Patrick MatthewsCommented:
Sorry, misread the question :)
0
Upgrade your Question Security!

Your question, your audience. Choose who sees your identity—and your question—with question security.

RiteshShahCommented:
declare @time datetime

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

if you run above script, it will deduct 2 days from current date and store that value in @time and you can show that value from @time varible.


0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
RiteshShahCommented:
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
0
goldmCommented:
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
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft SQL Server 2005

From novice to tech pro — start learning today.