title: Closest datetime for PostgreSQL 9.5
body: <p>I'm using PostgreSQL 9.5
  and I have a table like this:</p>
  
  <pre><code>CREATE TABLE tracks ( 
      track                bigserial  NOT NULL,
      time_track           timestamp,
      CONSTRAINT pk_aircraft_tracks PRIMARY KEY ( track )
   );
  </code></pre>
  
  <p>I want to obtain track for the closest value of datetime by SELECT operator.
  e.g, if I have:</p>
  
  <pre><code>  track    datatime
      1 | 2016-12-01 21:02:47 
      2 | 2016-11-01 21:02:47
      3 |2016-12-01 22:02:47
  </code></pre>
  
  <p>For input datatime 2016-12-01 21:00, the track is 2.</p>
  
  <p>I foud out <a href="https://stackoverflow.com/questions/6102824/is-there-a-postgres-closest-operator">Is there a postgres CLOSEST operator?</a> similar queston for integer.
  But it is not working with datatime or PostgreSQL 9.5 :</p>
  
  <pre><code>SELECT * FROM
  (
    (SELECT time_track, track FROM tracks WHERE time_track &gt;= now() ORDER BY time_track LIMIT 1) AS above
    UNION ALL
    (SELECT time_track, track FROM tracks WHERE time_track &lt; now() ORDER BY time_track DESC LIMIT 1) AS below
  ) 
  ORDER BY abs(?-time_track) LIMIT 1;
  </code></pre>
  
  <p>The error:</p>
  
  <pre><code>ERROR:  syntax error at or near "UNION"
  LINE 4:   UNION ALL
  </code></pre>