To get the records for the last 7 days, it would be something like:
select RecordDate, Id
from your_table
where RecordDate > DATEADD(day, -7, GETDATE())
To get the records for the last week only (within the last week not last 7 days)
select RecordDate, Id
from your_table
where RecordDate > DATEADD(day, -7, GETDATE())
and DATEPART(week, RecordDate) = DATEPART(week, GETDATE())
This query will return all records having a date later than 7 days before current date/time.
If you don't want the hours to be taken into account, then try something like this (works only on 2008 due to date datatype cast):
select RecordDate, Id
from your_table
where RecordDate > DATEADD(day, -7, cast(GETDATE() as date))
and DATEPART(week, RecordDate) = DATEPART(week, GETDATE())
Here's the version without the hours for 2005:
select RecordDate, Id
from your_table
where RecordDate > DATEADD(day, -7, CONVERT(datetime, CONVERT(char(10), GETDATE(),
101)))
and DATEPART(week, RecordDate) = DATEPART(week, GETDATE())
That's it :)
No comments:
Post a Comment