title: Get First and Last Day of Any Year
body: <p>I'm currently trying to get the first and last day of any year.  I have data from 1950 and I want to get the first day of the year in the dataset to the last day of the year in the dataset (note that the last day of the year might not be December 31rst and same with the first day of the year).</p>
  
  <p>Initially I thought I could use a <code>CTE</code> and call <code>DATEPART</code> with the day of the year selection, but this wouldn't partition appropriately.  I also tried a <code>CTE</code> self-join, but since the last day or first day of the year might be different, this also yields inaccurate results.</p>
  
  <p>For instance, using the below actually generates some <code>MIN</code>s in the <code>MAX</code> and vice versa, though in theory it should only grab the <code>MAX</code> date for the year and the <code>MIN</code> date for the year:</p>
  
  <pre><code>;WITH CT AS(
      SELECT Points
          , Date
          , DATEPART(DY,Date) DA
      FROM Table
      WHERE DATEPART(DY,Date) BETWEEN 363 AND 366
          OR DATEPART(DY,Date) BETWEEN 1 AND 3
  )
  SELECT MIN(c.Date) MinYear
      , MAX(c.Date) MaxYear
  FROM CT c
  GROUP BY YEAR(c.Date)
  </code></pre>