title: PostgreSQL - return string when testing integer with case statement
body: <p>When selecting some data from a table I'd like to use a <code>CASE</code> statement to return "n/a" in the event of a null value.</p>
  
  <p>This is a simplified but representative version of the table I'm looking at:</p>
  
  <pre class="lang-sql prettyprint-override"><code>CREATE TABLE test (
      id INTEGER NOT NULL,
      paid_cents INTEGER NULL DEFAULT NULL,
      PRIMARY KEY (id)
  );
  
  INSERT INTO test VALUES (1, 100);
  INSERT INTO test VALUES (2, NULL);
  INSERT INTO test VALUES (3, 0);
  </code></pre>
  
  <p>I'd expect to be able to use the following query:</p>
  
  <pre class="lang-sql prettyprint-override"><code>SELECT CASE
      WHEN paid_cents IS NULL THEN 'n/a'
      ELSE paid_cents / 100
      END AS "dollar amount"
  FROM test
  </code></pre>
  
  <p><a href="http://sqlfiddle.com/#!9/38c7f5/1/0" rel="nofollow noreferrer">A fiddle</a> of this behaves exactly as I'd expect. However trying this on my real database (PostgreSQL 9.4) results in an error:</p>
  
  <blockquote>
    <p>ERROR: invalid input syntax for integer: "n/a"<br>
    LINE 2: WHEN paid_cents IS NULL THEN 'n/a'</p>
  </blockquote>
  
  <p>It seems that Postgres is expecting an integer to be returned when testing an integer (and changing 'n/a' to a number does indeed work). I'm guessing that I'll need to <code>CAST</code> somewhere?</p>