Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

selecting records for the last 7 days 2

Status
Not open for further replies.
Dec 31, 2004
71
GB
Hi All,
Hope you can help - I have a query where i want to count records in a table where a date column's day is in the last 7 days, been trying to do it with datepart but can't seem to get it to function.

Can anyone give me an example?

Thanks
 
Code:
SELECT *
FROM YourTable 
WHERE DateColumn BETWEEN 
DATEADD(day, -6, CONVERT(DATETIME, (CONVERT(CHAR(8), GETDATE(), 112))))
AND 
CONVERT(DATETIME, (CONVERT(CHAR(8), GETDATE(), 112)))
 
To start....

Code:
Select Count(*)
From   YourTable
Where  DateColumn >= GetDate()

The GetDate() function returns date and time. If your values do not have a time component (i.e. all times are midnight), then you may want to try this:

Code:
Select Count(*)
From   YourTable
Where  DateColumn >= dateAdd(Day, DateDiff(Day, 0, GetDate()),0)




-George

"The great things about standards is that there are so many to choose from." - Fortune Cookie Wisdom
 
Sorry. I made a mistake.

Code:
Select Count(*)
From   YourTable
Where  DateColumn >= GetDate() - 7

Code:
Select Count(*)
From   YourTable
Where  DateColumn >= dateAdd(Day, DateDiff(Day, 0, GetDate()),0) - 7


-George

"The great things about standards is that there are so many to choose from." - Fortune Cookie Wisdom
 
thanks guys i have used gmmastros's example but have played with all

cheers for the quick replies as always

Nathan
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top