Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Selecting this month only

Status
Not open for further replies.

pwills

Technical User
Sep 14, 2002
54
GB
Is there a more efficient query than the following?

SELECT * FROM WIDGETS WHERE
(DATEPART(m, MADE_ON) = DATEPART(m, GETDATE())) AND (DATEPART(yy, MADE_ON) = DATEPART(yy, GETDATE()))
 
PS. In my case, I am using SQL Server 7
 
Actually, my need is not quite "this month" but "last month".

SELECT * FROM WIDGETS WHERE
(DATEPART(m, MADE_ON) = DATEPART(m, GETDATE())) AND (DATEPART(yy, MADE_ON) = DATEPART(yy, GETDATE()))

does not work because of January. There must be some staightforward method but I cannot find it in BOL.
 
Perhaps

SELECT * FROM WIDGETS WHERE
DATEDIFF ('m', MADE_ON,GETDATE()) = 1

would work?
 
does this work?

SELECT * FROM WIDGETS WHERE
(year(MADE_ON) = year(now()) AND month(MADE_ON) = month(now())-1) OR (year(MADE_ON) = year(now())-1 AND month(MADE_ON) = 12)
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top