[Webinar] Streamline your web hosting managementRegister Today

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 319
  • Last Modified:

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.

0
RustyZ32
Asked:
RustyZ32
  • 3
  • 2
1 Solution
 
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
Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

 
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
 
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

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 3
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now