Click here to Skip to main content
15,923,689 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,
I need to get last 3 months records from database.

For example, March is the current month (03/2012), i need data from March(03), February(02) and January(01) months.

please help
Posted
Comments
Dinesh.V.Kumar 13-Feb-14 9:18am    
use "<date> between GetDate() and DateAdd(m,GetDate(),-3)" in your query....
Challa92 13-Feb-14 10:05am    
no its not working..can u give write a query pls

select OrderDate from Orders where OrderDate
between GetDate() and Dateadd(yy,GetDate(),-3)

i got this errorfor the above query
Argument data type datetime is invalid for argument 2 of dateadd function.
Dinesh.V.Kumar 13-Feb-14 12:52pm    
It should not be "yy", it should be "mm"
select OrderDate from Orders where OrderDate
and I am sorry it should have been the other way round..

try this query...
select OrderDate from Orders where OrderDate
between Dateadd(yy,GetDate(),-3)
and GetDate()
Dinesh.V.Kumar 14-Feb-14 4:19am    
is it working??

1 solution

SQL
Select * from TableName
where dateColumn between dateadd(mm,-3,getdate()) and getdate()  
 
Share this answer
 
v2
Comments
Challa92 14-Feb-14 4:33am    
thank you:)

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900