June 14, 2006 at 2:33 am
Dear
i want to have a query which fetches all records which are inserted in last 7 days including today.
Please help me
Regards,
ASIF
June 14, 2006 at 2:38 am
is this from single table and do that table have date column?
Brij
June 14, 2006 at 3:46 am
Select * From TblName where <Date_Column> >=DateAdd(day,DateDiff(day,0,getdate()),-6)
or
Select * From TblName where <Date_Column> >DateAdd(day,DateDiff(day,0,getdate()),-7)
N 56°04'39.16"
E 12°55'05.25"
June 14, 2006 at 5:42 am
little simplified...
select * from tblname WHERE datecol>= DATEADD(DAY,-7,GETDATE())
Or
SELECT * FROM tblnameWHERE datecol BETWEEN dateadd(DAY,-7,GETDATE()) AND dateadd(day,1,GETDATE())
June 14, 2006 at 5:52 am
The original posting was inserted in last 7 days including today.
N 56°04'39.16"
E 12°55'05.25"
June 14, 2006 at 6:08 am
Daizy
Further your query will subtract only 7 days from current datetime, so if query is run at 10AM, then you will not get the records till 10 AM.
thanks
Brij
June 14, 2006 at 6:23 am
Yup!!
Missed the time issue.
Thanks
Daizy
June 14, 2006 at 6:39 am
WHERE DATEDIFF(day,[Date],GETDATE()) <= 7
but this will not utilise any index on the date column
whereas Peters solution will
Far away is close at hand in the images of elsewhere.
Anon.
June 14, 2006 at 2:19 pm
June 15, 2006 at 7:08 am
The only problem is with milliseconds for times of '23:59:59.003' to '23:59:59.997' which if present on day-8 then your query would include it.
Therefore you would to use CONVERT(varchar(12), DATEADD(day, -8, GETDATE()), 101) + ' 23:59:59.997'
or as I prefer how to do it
SET @startdate = DATEADD(day,DATEDIFF(day,0,GETDATE())-7,0)
...
WHERE [Date] >= @startdate
Far away is close at hand in the images of elsewhere.
Anon.
Viewing 10 posts - 1 through 9 (of 9 total)
You must be logged in to reply to this topic. Login to reply