How to Query over the past 7 days (C# VS2008)

Shaitan00

Well-known member
Joined
Aug 11, 2003
Messages
343
Location
Hell
I have a Database (Microsoft SQL Server Express 2005) that contains the following data table:

-DATE--------TIME----LEVEL
19/11/2008 - 8:00am - 05
19/11/2008 - 9:00am - 10
20/11/2008 - 8:00am - 06
20/11/2008 - 9:00am - 11
21/11/2008 - 8:00am - 12
...

This data can span as far back as 2-3 months...
I want to find a way to return a Dataset for each of the last 7 days. Writing these queries is pretty easy if I know the DATE corresponding to the last 7 days.

Is there anyway to calculate such a thing?
For example get Now(), then Now()-1, Now()-2, etc... or something similar?

Any help would be greatly appreciated.
Thanks,
 
This will give you today, and the previous six days:
Code:
for(int i = 0; i > -7; i--)
    DateTime.Now.AddDays(i);


If you are using SQL on a Database you may try something like this:
Code:
SELECT * FROM TABLE
WHERE DateField > StartDate AND DateField < EndDate;

Id probably go for something like the second option.
 
Back
Top