Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 basic question

Author  Topic 

gavakie
Posting Yak Master

221 Posts

Posted - 2007-09-21 : 10:57:34
I have a basic query select * From and has a column RecordDate I do I check just records for the last 6 months.

Kristen
Test

22859 Posts

Posted - 2007-09-21 : 11:05:47
So for today is that everything from march to the end of September?
i.e.:

SELECT [Now] = GetDate(),
[Start] = DATEADD(Month, DATEDIFF(Month, 0, GetDate()) - 6, 0),
[End] = DATEADD(Month, DATEDIFF(Month, 0, GetDate()) + 1, 0)

if so then you probably need something like:

SELECT *
FROM MyTable
WHERE RecordDate >= DATEADD(Month, DATEDIFF(Month, 0, GetDate()) - 6, 0)
AND RecordDate < DATEADD(Month, DATEDIFF(Month, 0, GetDate()) + 1, 0)

Kristen
Go to Top of Page

madhivanan
Premature Yak Congratulator

22864 Posts

Posted - 2007-09-24 : 02:16:56
If it it is based on today, then

SELECT *
FROM MyTable
WHERE RecordDate >= DATEADD(Month, -6, GetDate())


Madhivanan

Failing to plan is Planning to fail
Go to Top of Page
   

- Advertisement -