Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2005 Forums
 Transact-SQL (2005)
 select data that is 7 days older than current date

Author  Topic 

srucker
Starting Member

26 Posts

Posted - 2009-01-13 : 14:24:38
I only want to select data which is 7 days older than the current date.
How might I achieve such an extension to the where clause below??

Select Distinct CONVERT(nvarchar(510), oc.oc_name) AS Supervisor, wr.Res_Name as Resource,
CONVERT(nvarchar(510), rse.ResourceEnterpriseText1)AS Emp_number,
(Case when rse.ResourceEnterpriseOutlineCode2ID = 534 Then 'FTE'
when rse.ResourceEnterpriseOutlineCode2ID = 533 Then 'Contractor' end) AS Emp_type,
ad.Task_UID AS Task_ID, ad.Task_Name AS Task, ad.Work_Day AS Work_Day, ad.Hours AS Hours
from MSP_WEB_RESOURCES AS wr
JOIN MSP_VIEW_RES_ENT AS rse ON wr.res_euid = rse.ENT_ResourceUniqueID
JOIN MSP_OUTLINE_CODES AS oc ON oc.code_uid = rse.ResourceEnterpriseOutlineCode5ID
Left OUTER JOIN #actuals_data as ad on ad.res_name = wr.res_name
where rse.ResourceEnterpriseText2 like 'yes'
(and rse.ResourceEnterpriseDate1 > currentDate-7)

tkizer
Almighty SQL Goddess

38200 Posts

Posted - 2009-01-13 : 14:29:15
WHERE SomeDateTimeColumn <= GETDATE() - 7

Tara Kizer
Microsoft MVP for Windows Server System - SQL Server
http://weblogs.sqlteam.com/tarad/

Subscribe to my blog
Go to Top of Page

Skorch
Constraint Violating Yak Guru

300 Posts

Posted - 2009-01-13 : 15:03:30
I think what many people don't realize with GETDATE()-7 is that they're getting a date exactly 168 hours before the current timestamp rather than everything from the beginning of the day 7 days ago. If it is the latter that you're looking to do, then use this:
SELECT DATEADD(dd,DATEDIFF(dd,0,GETDATE()),-7)
Go to Top of Page
   

- Advertisement -