title: SQL CASE fails when using left() function
body: <p>I have this SQL <code>CASE</code> statement which checked that a value contains a comma and then performs the <code>left</code> and <code>charindex</code> function on it.</p>
  
  <p>However the query fails because it looks like its still trying to execute the <code>left()</code> function on it.</p>
  
  <pre><code>select 
     case 
        when '12560' like '%,%' 
          then left('12560',charindex(',', '12560', 0) - 1) 
          else '12560'  
     end
  </code></pre>
  
  <p>I basically may or may not have a comma in the parameter and therefore may or may not want to do a left on it.</p>
  
  <p>Can this be done in a single case statement? If not I will have to write function to do this with an IF statement.</p>
  
  <p>The error is:</p>
  
  <blockquote>
    <p>Msg 536, Level 16, State 1, Line 1<br>
    Invalid length parameter passed to the left function.</p>
  </blockquote>
  
  <p>Thanks</p>