Month to Date in SQL Server 2008

Posted by Aaron Smith on Stack Overflow See other posts from Stack Overflow or by Aaron Smith
Published on 2011-11-16T17:20:34Z Indexed on 2011/11/16 17:50 UTC
Read the original article Hit count: 232

Hopefully this will be an easy one to answer.

I am working on a table that requires MTD data. One of our SQL guys told me to use

MONTH (@monthtodate)= 11

Where @monthtodate is set to GetDate() in the parameter list in SQL Server Management Studio. So in "theory", he says, it should select the month (11) and then get today and return all the requested data in between those two dates. But I'm thinking this isn't correct.

In looking at my data I'm starting to think that It's just returning data for the whole month of November instead of just MTD. I guess, technically, anything that has 0 won't be calculated. However that just means it's poorly written code correct?

In your opinions, would this be the better way to return MTD data:

production_date <= @today and Production_Date >= DATEADD(mm, DATEDIFF(mm, 0, @today), 0)

Thanks in advance everyone!

© Stack Overflow or respective owner

Related posts about sql

Related posts about sql-server-2008