title: Order of SQL Server statements execution
body: <p>I have SQL server 2008 R2, windows 7 OS.</p>
  
  <p>Within the server, I have a table <code>con1</code> which was created by following SQL statement.</p>
  
  <pre><code>CREATE TABLE [dbo].[con1](
      [digit_str] [nvarchar](50) NULL
  ) ON [PRIMARY]
  </code></pre>
  
  <p>In the <code>con1</code> table, I have these values:</p>
  
  <pre><code> digit_str
  ----------------
      1
      1
      2
      3
      4
      5
      1.
  </code></pre>
  
  <p>I did executed following SQL statement against the database:</p>
  
  <pre><code>SELECT t1.digit FROM
  (
  select CAST(digit_str as int) as digit from con1 where RIGHT(digit_str,1) &lt;&gt; '.'
  ) as t1
  where t1.digit &lt;&gt; 1
  </code></pre>
  
  <p>the server given me following error message:</p>
  
  <blockquote>
    <p><em>Conversion failed when converting the nvarchar value '1.' to data type int.</em></p>
  </blockquote>
  
  <p>I thought my inner SQL executed first and created a temp table <code>t1</code>, so <code>1.</code> is not included in table <code>t1</code>, then SQL parser will use <code>where t1.digit &lt;&gt; 1</code> to filter temp table <code>t1</code>.</p>
  
  <p>But above seems not right, so any one explains execution order of above SQL?</p>