title: Why do I get datetime conversion out of range for 999 milliseconds?
body: <p>Could someone please explain me, why I get the error </p>
  
  <blockquote>
    <p>The conversion of a varchar data type to a datetime data type resulted
    in an out-of-range value.</p>
  </blockquote>
  
  <p>when I execute the following code on SQL Server 2017. I get the error only for .999 milliseconds</p>
  
  <pre><code>SELECT CAST('9999-12-31 23:59:59.999' AS DATETIME)
  </code></pre>
  
  <p>Result: </p>
  
  <blockquote>
    <p>The conversion of a varchar data type to a datetime data type resulted
    in an out-of-range value.</p>
  </blockquote>
  
  <p>Then I tried:</p>
  
  <pre><code>SELECT CAST('9999-12-31 23:59:59.998' AS DATETIME)
  </code></pre>
  
  <p>Result: 9999-12-31 23:59:59.997</p>
  
  <p>Then I tried:</p>
  
  <pre><code>SELECT CAST('9999-12-31 23:59:59.997' AS DATETIME)
  </code></pre>
  
  <p>Result: 9999-12-31 23:59:59.997</p>