title: How to filter SQL Server DateTime using .NET Core WebAPI with OData v4
body: <p>I am running a simple .NET Core WebApi application with OData Query v4 and SQL Server 2012.</p>
  
  <p>This works, but it's extremely slow:</p>
  
  <pre><code>GET /api-endpoint?$filter=date(MyDateTimeField) ge 2018-01-01&amp;$top=100
  </code></pre>
  
  <p>SQL Query generated by the URL above:</p>
  
  <pre><code>SELECT TOP 100 * FROM MyTable WHERE ((((DATEPART(year, [MyDateTimeField]) * 10000) + (DATEPART(month, [MyDateTimeField]) * 100)) + DATEPART(day, [MyDateTimeField])) &gt;= (((2018 * 10000) + (1 * 100)) + 1))
  </code></pre>
  
  <p>When I try to do this:</p>
  
  <pre><code>GET /api-endpoint?$filter=MyDateTimeField ge 2018-01-01T00:00:00.00Z&amp;$top=100
  </code></pre>
  
  <p>It generates the following SQL query:</p>
  
  <pre><code>SELECT TOP 100 * FROM MyTable WHERE [MyDateTimeField] &gt; '2018-01-01T00:00:00.0000000'
  </code></pre>
  
  <p>Which returns this error:</p>
  
  <blockquote>
    <p>Conversion failed when converting date and/or time from character
    string.</p>
  </blockquote>
  
  <p><strong>What would the OData Query syntax be to generate a SQL query similiar to this?</strong></p>
  
  <pre><code>SELECT TOP 100 * FROM MyTable WHERE [MyDateTimeField] &gt; '2018-01-01'
  </code></pre>