title: SQL Server Query Compilation - Converting VarChar to Float
body: <p>I am getting some strange behaviour from SQL Server. I have an underlying view that returns an amount field. That amount field is casting from VARCHAR(50) to FLOAT.</p>
  
  <p>I can <strong>successfully</strong> run the following query:</p>
  
  <pre><code>SELECT AMOUNT
  FROM vReturnDetails
  </code></pre>
  
  <p>I can also <strong>successfully</strong> run the following query:</p>
  
  <pre><code>SELECT AMOUNT
  FROM vReturnDetails v 
      INNER JOIN MyTable t 
          ON t.LicenceNumber = v.LicenceNumber
          AND t.ReturnYear = v.ReturnYear 
  WHERE t.ReturnYear = 2012
  </code></pre>
  
  <p>Similarly I <strong>can</strong> run this query:</p>
  
  <pre><code>SELECT AMOUNT
  FROM vReturnDetails v 
      INNER JOIN MyTable t 
          ON t.LicenceNumber = v.LicenceNumber
          AND t.ReturnYear = v.ReturnYear 
  WHERE t.ReturnYear &lt;&gt; 2012
  </code></pre>
  
  <p>This is where it gets strange though, I <strong>cannot</strong> run either of the following:</p>
  
  <pre><code>SELECT AMOUNT
  FROM vReturnDetails v 
      INNER JOIN MyTable t 
          ON t.LicenceNumber = v.LicenceNumber
          AND t.ReturnYear = v.ReturnYear 
  WHERE t.ReturnYear = 2012 OR t.ReturnYear &lt;&gt; 2012
  
  SELECT AMOUNT
  FROM vReturnDetails v 
      INNER JOIN MyTable t 
          ON t.LicenceNumber = v.LicenceNumber
          AND t.ReturnYear = v.ReturnYear 
  </code></pre>
  
  <p>I can <strong>successfully</strong> run this query though:</p>
  
  <pre><code>SELECT AMOUNT
  FROM vReturnDetails v 
      INNER JOIN MyTable t 
          ON t.LicenceNumber = v.LicenceNumber
          AND t.ReturnYear = v.ReturnYear 
  WHERE t.ReturnYear &lt;&gt; 2013 --2013 is a dummy value that doesn't exist
  </code></pre>
  
  <p>Can anyone shed any light on what is happening under the covers here. It makes no sense at all to me so I'm not sure where to start looking to fix the problem.</p>
  
  <p>The error I am receiving is </p>
  
  <p><em>Error converting data type varchar to float.</em></p>
  
  <p>This is on Sql Server 2008 SP2</p>