Saturday, February 25, 2012

Records last month

Today's month is 9 as in September. How can I retrieve records entered last month?

This is what I got so far...

WHERE (Classes.CLWhenDt >= DATEADD([MONTH], - 1, GETDATE()))

WHERE MONTH(Classes.CLWhenDt)=8

OR

WHERE Classes.CLWhenDt > '07/31/2006' AND Classes.CLWhenDt <'09/01/2006'

|||

OR

WHERE DATEDIFF(m,Classes.CLWhenDT,getutcdate())=1

Or this will perform faster:

WHERE Classes.CLWhenDT>=DATEADD(m,DATEDIFF(m,0,getutcdate())-1,0) AND Classes.CLWhenDT<DATEADD(m,DATEDIFF(m,0,getutcdate()),0)

|||Thanks for all your replies. I used this one "WHERE Classes.CLWhenDt > '07/31/2006' AND Classes.CLWhenDt <'09/01/2006'" as a temp solution but I will try your reply Motley.|||

Actually, that will catch dates that part way through 7/31. You should use:

WHERE Classes.CLWhenDt>='08/01/2006' AND Classes.CLWhenDt<'09/01/2006' instead, unless you are sure that CLWhenDt never contains a time component (aka always set to midnight).

No comments:

Post a Comment