title: Arithmetic overflow error converting expression to data type datetime sql
body: <p>I cannot figure out where I've messed this up. Full code:</p>
  
  <pre><code>DECLARE @start_date DATETIME, @end_date DATETIME
  DECLARE @Table TABLE (StartDate DATETIME, Enddate DATETIME, WeekNo INT)
  
  SET @start_date = '2017-01-01'
  SET @end_date =   '2017-12-31'
  
  INSERT INTO @Table 
      SELECT 
          MIN(dt), MAX(dt), w
      FROM
          (SELECT 
               dt, year(dt) y, DATEPART(week, dt) w
           FROM
               (SELECT 
                    @start_date + (ROW_NUMBER() OVER (ORDER BY (SELECT NULL)) - 1) dt
                FROM 
                    sys.columns s1 
                CROSS JOIN
                    sys.columns s2) q
           WHERE 
               dt BETWEEN @start_date AND @end_date) a
  GROUP BY 
      y, w
  </code></pre>
  
  <p>I've narrowed down where the error is happening. It happens here:</p>
  
  <pre><code>SELECT 
      dt, year(dt) y, DATEPART(week, dt) w
  FROM
      (SELECT 
           @start_date + (ROW_NUMBER() OVER (ORDER BY (SELECT NULL)) - 1) dt
       FROM 
           sys.columns s1 
       CROSS JOIN
           sys.columns s2) q
  WHERE 
      dt BETWEEN @start_date AND @end_date
  </code></pre>
  
  <p>If I run just this part I get the error:</p>
  
  <blockquote>
    <p>Arithmetic overflow error converting expression to data type datetime.</p>
  </blockquote>
  
  <p>I've tried adding in some <code>CONVERT</code> here and there trying to find where it's messing up, but I can not figure it out. </p>
  
  <p>I have seen a couple of others who had a similar problem, but neither <a href="https://stackoverflow.com/questions/12599167/arithmetic-overflow-error-when-converting-expression-to-data-type-datetime">this one</a> or <a href="https://www.google.com/url?sa=t&amp;rct=j&amp;q=&amp;esrc=s&amp;source=web&amp;cd=1&amp;cad=rja&amp;uact=8&amp;ved=0ahUKEwjp9Oz9sbnWAhXD44MKHWRGDV4QFggoMAA&amp;url=https%3A%2F%2Fstackoverflow.com%2Fquestions%2F18416623%2Farithmetic-overflow-error-converting-expression-to-data-type-datetime-while-di&amp;usg=AFQjCNEFmfJulEzmgHa_enUK-11UAHLmnQ" rel="nofollow noreferrer">this one</a> have any solution that is helpful to me.</p>
  
  <p>I'm new to the set based SQL programming, I'm self-taught in loops so this is out of my depth. Any help is appreciated. </p>