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
 Get current month and previous 2 months

Author  Topic 

PUTTU PATIL
Starting Member

7 Posts

Posted - 2014-02-10 : 23:58:58
I have table 'Open_Months' as shown below

Emp_Id Month Year
00FBG 4 2013
I need current month and previous 2 months, for this i have concatinated month and year column into date formate, the query for this as shown below

SELECT cast(CONVERT(datetime, CONVERT(varchar(4), OM_Year) + RIGHT('0' + CONVERT(varchar(2), OM_Month), 2) + '01', 112) AS Datetime) from Open_Months where OM_Emp_ID = '00FBG'

Date
2013-04-01
from the above date i need to find current month and its previous 2 months.



Regards,

AbhiShek Patil

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2014-02-11 : 00:19:06
[code]
select * from Open_Months where Emp_ID = '00FBG'
and [Year]=YEAR(GETDATE())
and [Month] BETWEEN MONTH(DATEADD(mm,-2,GETDATE())) AND MONTH(GETDATE())
[/code]

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/
https://www.facebook.com/VmBlogs
Go to Top of Page
   

- Advertisement -