title: CASE THEN clause always evaluated
body: <p>I'm doing a <code>SELECT</code> which uses <code>CASE</code> to convert nvarchar values into a proper type, something like this:</p>
  
  <pre><code>SELECT CASE 
      WHEN @propType = 'money' THEN convert(money, datavalue)
      [...]
      ELSE datavalue
  END
  FROM [...]
  </code></pre>
  
  <p>However, it seems the <code>convert</code> is always executed, even when <code>@propType</code> is <em>not</em> equal to money. Runnable example:</p>
  
  <pre><code>declare @proptype nvarchar(50)= 'nvarchar'
  declare @val nvarchar(10) = 'test'
  select 
      case @proptype
          when 'money' then convert(money, @val)
          else @val
      end
  </code></pre>
  
  <p>Why is this, and how can I get around it? The MSDN documentation says this:</p>
  
  <blockquote>
    <p>The CASE statement evaluates its conditions sequentially and stops
    with the first condition whose condition is satisfied. In some
    situations, an expression is evaluated before a CASE statement
    receives the results of the expression as its input. Errors in
    evaluating these expressions are possible. Aggregate expressions that
    appear in WHEN arguments to a CASE statement are evaluated first, then
    provided to the CASE statement. For example, the following query
    produces a divide by zero error when producing the value of the MAX
    aggregate. This occurs prior to evaluating the CASE expression.</p>
  </blockquote>
  
  <p>I'm not sure this is relevant, but the language is somewhat heavy for a non-native, so maybe it is?</p>