March 4, 2010 at 10:20 am
I have a table called order and i have a field called orderdate as datetime
i want to check if the orderdate falls between the last 13 months from today's date. how do i check that.
This is how it stores the info in date 8/7/2008 11:35:04 AM
March 4, 2010 at 10:32 am
SELECT col
FROM table
WHERE orderdate >= DATEADD(month,-13,getdate())
AND orderdate < getdate ()
or, if you'd need to include the full month:
SELECT col
FROM table
WHERE orderdate >= DATEADD(month,DATEDIFF(month,0,getdate())-13,0)
AND orderdate < getdate ()
March 4, 2010 at 10:35 am
For the last 13 months, I would use
where datediff(mm, OrderDate, getdate())<=13
edit: Mine will include the whole month, so for today it would have 13 months and 4 days to show all of Feb 2009.
March 4, 2010 at 10:41 am
adams.squared (3/4/2010)
For the last 13 months, I would usewhere datediff(mm, OrderDate, getdate())<=13
edit: Mine will include the whole month, so for today it would have 13 months and 4 days to show all of Feb 2009.
When coding that way you prevent SQL server from using possibly existing indexes on OrderDate.
March 4, 2010 at 10:45 am
lmu92 (3/4/2010)
When coding that way you prevent SQL server from using possibly existing indexes on OrderDate.
I was not aware of that. Thanks
Viewing 5 posts - 1 through 4 (of 4 total)
You must be logged in to reply to this topic. Login to reply