Click to See Complete Forum and Search --> : How to Query over the past 7 days (C# VS2008)


Shaitan00
11-19-2008, 10:08 PM
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,

MikeOS
11-20-2008, 03:42 PM
SELECT *
FROM yourtablename
WHERE DATEDIFF(Day, Date, GETDATE()) <= 7

I'm not quite sure why you have separate fields for date and time but as long as your Date field is a proper DateTime field then the above should work fine (as long as you put in your table name of course).