title: Spark SQL: cast a variable from a case when
body: <p>How do I change the datatype of variable from a case when statement in spark SQL? I can't find any documentation for this anywhere</p>
  <p>For e.g. I would have thought the below would work:</p>
  <pre><code>SELECT cast(CASE WHEN my_var = 'one'
              THEN null
              ELSE 0
              END AS new_var as Integer)
  FROM my_table    
  </code></pre>
  <p>I get a syntax error when I try this.</p>
  <p>I have also tried:</p>
  <pre><code>SELECT      CASE WHEN my_var = 'one'
              THEN null
              ELSE 0
              END AS cast(new_var as Integer)
  FROM my_table    
  </code></pre>