sql – Postgres:如何从unix纪元转换为日期?

前端之家收集整理的这篇文章主要介绍了sql – Postgres:如何从unix纪元转换为日期?前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
这个声明给了我日期和时间.

我如何修改语句,以便它只返回日期?

SELECT to_timestamp( TRUNC( CAST( epoch_ms AS bigint  ) / 1000 ) );

解决方法

/* 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

猜你在找的MsSQL相关文章