title: Get first day of week in SQL Server
body: <p>I am trying to group records by week, storing the aggregated date as the first day of the week. However, the standard technique I use for rounding off dates does not appear to work correctly with weeks (though it does for days, months, years, quarters and any other timeframe I've applied it to).</p>
  
  <p>Here is the SQL:</p>
  
  <pre><code>select "start_of_week" = dateadd(week, datediff(week, 0, getdate()), 0);
  </code></pre>
  
  <p>This returns <code>2011-08-22 00:00:00.000</code>, which is a Monday, not a Sunday. Selecting <code>@@datefirst</code> returns <code>7</code>, which is the code for Sunday, so the server is setup correctly in as far as I know.</p>
  
  <p>I can bypass this easily enough by changing the above code to:</p>
  
  <pre><code>select "start_of_week" = dateadd(week, datediff(week, 0, getdate()), -1);
  </code></pre>
  
  <p>But the fact that I have to make such an exception makes me a little uneasy. Also, apologies if this is a duplicate question. I found some related questions but none that addressed this aspect specifically.</p>