it-swarm.com.de

PostgreSQL: Wie konvertiere ich von Unix-Epoche auf Datum?

Die Aussage gibt mir das Datum und die Uhrzeit.

Wie kann ich die Anweisung so ändern, dass nur das Datum (und nicht die Uhrzeit) zurückgegeben wird?

SELECT to_timestamp( TRUNC( CAST( Epoch_ms AS bigint ) / 1000 ) );
37
sid_com
/* Current time */
 select now(); 

/* Epoch from current time;
   Epoch is number of seconds since 1970-01-01 00:00:00+00 */
 select extract(Epoch from now()); 

/* Get back time from Epoch */
 -- Option 1 - use to_timestamp function
 select to_timestamp( extract(Epoch from now()));
 -- Option 2 - add seconds to 'Epoch'
 select timestamp with time zone 'Epoch' 
         + extract(Epoch from now()) * interval '1 second';

/* Cast timestamp to date */
 -- Based on Option 1
 select to_timestamp(extract(Epoch from now()))::date;
 -- Based on Option 2
 select (timestamp with time zone 'Epoch' 
          + extract(Epoch from now()) * interval '1 second')::date; 

 /* For column Epoch_ms */
 select to_timestamp(extract(Epoch epoch_ms))::date;

PostgreSQL Docs

65
Tomas Greif