sql query

D

dave

Hi there!!
I have one table with date field in sql server2000.
I want to write one sql query statement that fetch
last three months data from the current month..

Any help would be appreciated...
Thanx..
dave
 
A

Aaron Bertrand [MVP]

SELECT unknown_columns
FROM unknown_table
WHERE unknown_date_column >= DATEADD(MONTH, -3, GETDATE())
 
D

dave

Thanx
but i 'm just wondering whther the query I have written
will give me the same result or not...

Select * From tblPR_MonthData WHERE DealerCode LIKE '%'
AND DATEDIFF(Month,MonthName,getdate()) < 3 Order By
MonthName DESC

where MonthName is field name...

Thanx again..
dave
 
D

dave

Thanx Aaron
I tried your query as well and it works same.
but the problem is
if the current month is january it fetches
last year december, and jan,feb,march and april of
current year..
actually it should display jan of current year and nov,
dec of last year..

dave
 
A

Aaron Bertrand [MVP]

What does LIKE '%' offer to the query? And I find DATEDIFF slightly less
intuitive...
 
A

Aaron Bertrand [MVP]

I tried your query as well and it works same.
but the problem is
if the current month is january it fetches
last year december, and jan,feb,march and april of
current year..

This is part of the problem with not giving proper specs. You never
mentioned that you had data from the future in the table, nor did you
provide sample data, etc.

Anyway, I don't understand how the query could have missed data for
November, if in fact your table had data from the correct window in
November. If you want the whole month of November, you need to state that,
instead of saying "last 3 months." To me, that means today minus three
months, not this month, last month, and the month before.

And if you want to exclude data from the future, you add another WHERE
clause. So instead of just WHERE dateColumn >= someDate you add AND
dateColumn < someOtherDate.

Hopefully this answers your questions. If you want better answers, please
provide better specs. See http://www.aspfaq.com/5006
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,770
Messages
2,569,583
Members
45,075
Latest member
MakersCBDBloodSupport

Latest Threads

Top