title: SQL to get number of working days between two dates
body: <p>I need to get number of "Working days" between any 2 given dates, and for that purpose i tried this query, which works perfectly, but I can't understand a part of it. Please anyone explain it to me:</p>
  
  <pre><code>SELECT (DATEDIFF(dd, '2015-01-04', '2015-01-10') + 1)-
          (DATEDIFF(wk, '2015-01-04', '2015-01-10') * 2)-
          (CASE WHEN DATENAME(dw, '2015-01-04') = 'Sunday' THEN 1 ELSE 0 END)-
          (CASE WHEN DATENAME(dw, '2015-01-10') = 'Saturday' THEN 1 ELSE 0 END)
  </code></pre>
  
  <p>I know this much:</p>
  
  <ul>
  <li>First line gets all days inclusive the given dates.</li>
  <li>Second line check number of weeks falling in between these days, so subtracts the required weekends from the total days</li>
  </ul>
  
  <p>Now I'm confused about the last two lines. It check if first date is a Sunday then subtract 1 from total, likewise for the last date to be a Saturday. <strong>But what if first date is a SATURDAY and Last date is SUNDAY?</strong></p>