title: SQL Server: Finding date given EndDate and # Days, excluding days from specific date ranges
body: <p>I have a <code>TableA</code> in a database similar to the following: </p>
  
  <pre><code>Id | Status   | Start       | End
  
  1  | Illness  | 2013-04-02  | 2013-04-23
  2  | Illness  | 2013-05-05  | 2014-01-01
  3  | Vacation | 2014-02-01  | 2014-03-01
  4  | Illness  | 2014-03-08  | 2014-03-09
  5  | Vacation | 2014-05-05  | NULL
  </code></pre>
  
  <p>Imagine it's keeping track of a specific user's "Away" days. Given the following Inputs: </p>
  
  <ol>
  <li><code>SomeEndDate</code> (Date), </li>
  <li><code>NumDays</code> (Integer)</li>
  </ol>
  
  <p>I want to find the <code>SomeStartDate</code> (Date) that is <code>Numdays</code> non-illness days from <code>EndDate</code>. In other words, say I am given a <code>SomeEndDate</code> value '2014-03-10' and a <code>NumDays</code> value of 60; the matching SomeStartDate would be:</p>
  
  <ol>
  <li>2014-03-10 to 2014-03-09 = 1</li>
  <li>2014-03-08 to 2014-01-01 = 57</li>
  <li>2013-05-05 to 2013-05-03 = 2</li>
  </ol>
  
  <p>So, at 60 non-illness days, we get a <code>SomeStartDate</code> of '2013-05-03'. IS there any easy way to accomplish this in SQL? I imagine I could loop each day, check whether or not it falls into one of the illness ranges, and increment a counter if not (exiting the loop after counter = @numdays)... but that seems wildly inefficient. Appreciate any help.</p>