加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 站长学院 > MsSql教程 > 正文

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

发布时间:2020-12-12 16:39:35 所属栏目:MsSql教程 来源:网络整理
导读:这个声明给了我日期和时间. 我如何修改语句,以便它只返回日期? 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
这个声明给了我日期和时间.

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

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

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读