Select Where Month= X and Year= Y

Khrys picture Khrys · May 10, 2013 · Viewed 16.3k times · Source

I have the following select:

SELECT * FROM Table
WHERE Column= datepart(month,'2013-05-07 10:18:00') 
  AND Column= datepart(year,'2013-05-07 10:18:00')

I want it to show me itens where month= 05 and year= 2013. Actually it isn't showing anything (but there is results to be show). Thanks.

Answer

Kevin Kunderman picture Kevin Kunderman · May 10, 2013

I would use the MONTH and YEAR functions

SELECT * FROM Table
WHERE MONTH(yourColumn) = 5  and YEAR(yourColumn) = 2013

http://msdn.microsoft.com/en-us/library/ms186313.aspx

http://msdn.microsoft.com/en-us/library/ms187813.aspx