title: SMALLINT to VARCHAR conversion in SELECT / CASE statement in SQL query
body: <p>I am working on a code that takes a decimal output and changes it to an abbreviated hex output. However, I get an error message when I attempt to convert the smallint format to varchar. When I run the code below, I get the error message</p>
  
  <blockquote>
    <p>Msg 245, Level 16, State 1, Line 1<br>
    Conversion failed when converting the varchar value 'E' to data type smallint</p>
  </blockquote>
  
  <p>I have tried converting the <code>SELECT</code> statement as:</p>
  
  <pre><code>SELECT 
      *, 
      CONVERT(VARCHAR, PCM_ID1)
  </code></pre>
  
  <p>But this returns the same error message. I have also modified the code to </p>
  
  <pre><code>...
  WHEN PCM_ID1 = '10' THEN CAST('A' AS VARCHAR)
  WHEN PCM_ID1 = '11' THEN CAST('B' AS VARCHAR)
  WHEN PCM_ID1 = '12' THEN CAST('C' AS VARCHAR)
  WHEN PCM_ID1 = '13' THEN CAST('D' AS VARCHAR)
  WHEN PCM_ID1 = '14' THEN CAST('E' AS VARCHAR)
  WHEN PCM_ID1 = '15' THEN CAST('F' AS VARCHAR)
  ...
  </code></pre>
  
  <p>and this also returns the same error message. The original code sample is below:</p>
  
  <pre><code>SELECT 
      *, 
  FROM
      dbo.DMV_All_Tests
  CROSS APPLY 
      (SELECT
           CASE
              WHEN PCM_ID1 = '0' THEN '0'
              WHEN PCM_ID1 = '1' THEN '1'
              WHEN PCM_ID1 = '2' THEN '2'
              WHEN PCM_ID1 = '3' THEN '3'
              WHEN PCM_ID1 = '4' THEN '4'
              WHEN PCM_ID1 = '5' THEN '5'
              WHEN PCM_ID1 = '6' THEN '6'
              WHEN PCM_ID1 = '7' THEN '7'
              WHEN PCM_ID1 = '8' THEN '8'
              WHEN PCM_ID1 = '9' THEN '9'
              WHEN PCM_ID1 = '10' THEN 'A'
              WHEN PCM_ID1 = '11' THEN 'B' 
              WHEN PCM_ID1 = '12' THEN 'C'
              WHEN PCM_ID1 = '13' THEN 'D'
              WHEN PCM_ID1 = '14' THEN 'E'
              WHEN PCM_ID1 = '15' THEN 'F'
              WHEN PCM_ID1 = '16' THEN '10'
              ELSE PCM_ID1
           END AS [PCM ID1 Dec2Hex]) ca
  WHERE
      KeyDateTime BETWEEN '2018/11/01' AND '2018/12/01' 
      AND ca.[PCM ID1 Dec2Hex] = '1'
  </code></pre>
  
  <p>My goal is to complete a successful conversion, so that instead of '14' as PCM_ID1 is stored in the database, it would give me the hex equivalent, either as 'F' or '0F'. This would be used to cross-check with our other database, which stores these PCM_ID1 values as hex. </p>