title: How to COALESCE a timestamp column?
body: <p>In PostgreSQL 9.3 I have the following table with 2 timestamps:</p>
  
  <pre><code>create table pref_users (
          id varchar(32) primary key,
          first_name varchar(64) not null,
          last_name varchar(64),
          female boolean,
          avatar varchar(128),
          city varchar(64),
          mobile varchar(64),
          login timestamp default current_timestamp,
          logout timestamp,
          last_ip inet,
          vip timestamp,       /* XXX can be NULL */
          grand timestamp,     /* XXX can be NULL */
          mail varchar(256),
          green integer,
          red integer,
          medals integer not null default 0
  );
  </code></pre>
  
  <p>The timestamps <code>vip</code> and <code>grand</code> indicate if the users of my game have paid for certain privilleges - until those dates.</p>
  
  <p>When a user connects to my game server, I call the following procedure with <code>OUT</code> parameters:</p>
  
  <pre><code>create or replace function pref_get_user_info(
      IN _id varchar,
      OUT is_banned boolean,
      OUT is_grand boolean,
      OUT is_vip boolean,
      OUT rep integer
  ) as $BODY$
          begin
              is_banned := exists(select 1 from pref_ban where id=_id);
  
              if is_banned then
                  return;
              end if;
  
              select
                  grand &gt; current_timestamp,
                  vip &gt; current_timestamp,
              into is_grand is_vip
              from pref_users where id=_id;
  
              if is_grand or is_vip then
                  return;
              end if;
  
              select
                  count(nullif(nice, false)) -
                  count(nullif(nice, true))
              into rep
              from pref_rep where id=_id;
          end;
  $BODY$ language plpgsql;
  </code></pre>
  
  <p>This does work well, but sometimes delivers <code>NULL</code> values to my game daemon (to a Perl script):</p>
  
  <pre><code># select * from pref_get_user_info('OK674418426646');
   is_banned | is_grand | is_vip | rep
  -----------+----------+--------+-----
   f         |          |        | 126
  (1 row)
  </code></pre>
  
  <p>I don't need a <code>NULL</code> though (and it prints a warning in my Perl script) - I just need a "true" or "false" values there.</p>
  
  <p>So I have tried:</p>
  
  <pre><code>select
      coalesce(grand, 0) &gt; current_timestamp,
      coalesce(vip, 0) &gt; current_timestamp,
  into is_grand is_vip
  from pref_users where id=_id;
  </code></pre>
  
  <p>But this gives me error:</p>
  
  <pre><code># select * from pref_get_user_info('OK674418426646');
  ERROR:  COALESCE types timestamp without time zone and integer cannot be matched
  LINE 2:                         coalesce(grand, 0) &gt; current_timesta...
                                                  ^
  QUERY:  select
                          coalesce(grand, 0) &gt; current_timestamp,
                          coalesce(vip, 0) &gt; current_timestamp,
                                    is_vip
                      from pref_users where id=_id
  CONTEXT:  PL/pgSQL function pref_get_user_info(character varying) line 9 at SQL statement
  </code></pre>
  
  <p>So I wonder what to do here please?</p>
  
  <p>Do I really have to</p>
  
  <pre><code>select
      coalesce(grand, current_timestamp - interval '1 day') &gt; current_timestamp,
      coalesce(vip, current_timestamp - interval '1 day') &gt; current_timestamp,
  into is_grand is_vip
  from pref_users where id=_id;
  </code></pre>
  
  <p>or is there maybe a nicer way (like maybe "epoch start" or "yesterday")? </p>
  
  <p><strong>UPDATE:</strong></p>
  
  <p>As suggested by Clodoaldo Neto (thanks!) I've tried:</p>
  
  <pre><code>select
  coalesce(grand &gt; current_timestamp, false),
  coalesce(vip &gt; current_timestamp, false),
  into is_grand is_vip
  from pref_users where id=_id;
  </code></pre>
  
  <p>but <code>is_vip</code> is NULL when <code>vip</code> is NULL:</p>
  
  <pre><code># select * from pref_get_user_info('OK674418426646');
   is_banned | is_grand | is_vip | rep
  -----------+----------+--------+-----
   f         | t        |        |
  (1 row)
  </code></pre>
  
  <p>And when I try either of the following I get syntax error:</p>
  
  <pre><code>select
  coalesce(grand &gt; current_timestamp, false),
  coalesce(vip &gt; current_timestamp, false),
  into is_grand, is_vip
  from pref_users where id=_id;
  
  select
  coalesce(grand &gt; current_timestamp, false),
  coalesce(vip &gt; current_timestamp, false),
  into (is_grand, is_vip)
  from pref_users where id=_id;
  </code></pre>
  
  <p>How can I <code>SELECT</code> into 2 variables at once here?</p>