Won't MONTH(DATEADD(MM,-1,@DATEONE)) give you a bit of grief whilst you are in January (=1) and comparing with December (=12)?I reckon you could do something like:-- "113" format is "31 Dec 2004 ..." styleSELECT @StartOfThisMonth = '01' + SUBSTRING(CONVERT(varchar(24), @MyDate, 113), 3, 24)SELECT @StartOfNextMonth = DATEADD(Month, 1, @StartOfThisMonth)SELECT *FROM MyTableWHERE MyDateColumn >= @StartOfThisMonth AND MyDateColumn < @StartOfNextMonth
Note that the END point is the FIRST day of the FOLLOWING month, and I select LESS THAN this. That allows for a value of 1 minute before midnight on the last day of the month to be included; and I don't have to know how many days in the month etc.I can addSELECT @StartOfLastMonth = DATEADD(Month, -1, @StartOfThisMonth)
and then my WHERE for last month is:WHERE MyDateColumn >= @StartOfLastMonth AND MyDateColumn < @StartOfThisMonth
Kristen