title: Is there a way to set variables in a Select statement that can be used in the same statements WHERE clause?
body: <p>I'm looking for a way to set a variable in a Select statement so it can be later used in the <code>where</code> clause.</p>
  
  <p>Here is an example:</p>
  
  <pre><code>SELECT DateDiff(d,Time_Stamp,GetDate()) as Var1 FROM Report Where Var1 = 5
  </code></pre>
  
  <p>I noticed that the <code>ORDER BY Var1</code> clause, works fine, but when I try to use it in a where statement it doesn't work fine, it gives an error on the <code>Var1</code> column.</p>
  
  <p>This doesn't work either because it doesn't return any rows.</p>
  
  <pre><code>DECLARE @Var1 int
  SELECT @Var1 = DateDiff(d,Time_Stamp,GetDate()) FROM Report Where @Var1 = 5
  </code></pre>
  
  <p>If this does make sense to you, can you tell me what you would do in this case so you don't have to write redundant code like this...</p>
  
  <pre><code>SELECT DateDiff(d,Time_Stamp,GetDate()) FROM Report 
   Where DateDiff(d,Time_Stamp,GetDate()) = 5
  </code></pre>
  
  <p>?</p>