title: Avoid Repeated Case When
body: <p>How can I avoid having to type the same <code>CASE WHEN...</code> over and over in a <code>WHERE</code> clause? </p>
  
  <p><code>CASE WHEN rawGlass.blockHeight &gt; rawGlass.blockWidth THEN rawGlass.blockWidth ELSE rawGlass.blockHeight END</code> repeats quite frequently in this query. I'd like to only have to do that once. I already know referencing <code>[Glass Height]</code> in the <code>WHERE</code> clause will result in an <code>Invalid column...</code> error. </p>
  
  <p>I realize there may be other solutions that don't involve using a <code>CASE WHEN</code> that will work for this particular instance and I welcome them, but I would really like an answer to the question at hand since I've come across this problem before with other queries. </p>
  
  <pre><code>SELECT
      parts.pgwName AS [Part Name],
      parts.active AS [Active],
      cutVinyl.boardName AS [Vinyl Board],
      cutVinyl.rollWidth AS [Roll Width],
      CASE WHEN rawGlass.blockHeight &gt; rawGlass.blockWidth THEN rawGlass.blockWidth ELSE rawGlass.blockHeight END AS [Glass Height]
  FROM
      parts
          LEFT JOIN cutVinyl ON parts.vinylBoard = cutVinyl.boardName
          LEFT JOIN rawGlass ON parts.outerSku = rawGlass.sku
  WHERE
      (
          (cutVinyl.stretchRadius IS NOT NULL OR cutVinyl.stretchRadius = 0) AND 
          cutVinyl.rollWidth &lt; (CASE WHEN rawGlass.blockHeight &gt; rawGlass.blockWidth THEN rawGlass.blockWidth ELSE rawGlass.blockHeight END)
      ) ...
  </code></pre>
  
  <p><br /></p>
  
  <p><em><strong>Note:</strong> The list of conditions goes on a while, so wrapping the whole <code>WHERE</code> clause in one <code>CASE WHEN...</code> statement and then repeating the conditions twice doesn't really help any.</em></p>