title: Why is SQL Server returning a different order when using 'month' in 'where'?
body: <p>I run a procedure call that calculates sums into table rows. First I taught the procedure is not working as expected, so I wasted half a day trying to fix what actually works fine.</p>
  
  <p>Later I actually taken a look at the <code>SELECT</code> that gets the data on screen and was surprised by this:</p>
  
  <pre><code>YEAR(M.date) = 2016
  --and MONTH(M.date) = 2
  </code></pre>
  
  <p>and </p>
  
  <pre><code>YEAR(M.date) = 2016
  and MONTH(M.date) = 2
  </code></pre>
  
  <p><a href="https://i.stack.imgur.com/wC4oZ.jpg" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/wC4oZ.jpg" alt="Screenshot"></a></p>
  
  <p>So the second example returns a different sorting than the first.</p>
  
  <p>The thing is I do calculations on the whole year. Display data on year + month parameters.</p>
  
  <p>Can someone explain why this is happening and how to avoid this?</p>
  
  <p>In my procedure that calls the <code>SELECT</code> for on screen data I have it implemented like so:</p>
  
  <pre><code>and (@month = 0 or (month(M.date) = @month))
  and year(M.date) = @year
  </code></pre>
  
  <p>So the month parameter is optional if the user wants to see the data for the whole year and year parameter is mandatory.</p>