PostgreSQL: Unix エポックから日付に変換するには? 質問する

PostgreSQL: Unix エポックから日付に変換するには? 質問する

明細書には日付と時刻が記載されています。

日付のみ(時刻は返さない)を返すようにステートメントを変更するにはどうすればよいでしょうか?

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

ベストアンサー1

関数を使用してto_timestamp、タイムスタンプをキャストしますdate

 select to_timestamp(epoch_column)::date;

cast代わりにもっと標準的なものを使うこともできます::

select cast(to_timestamp(epoch_column) as date);

詳細:

/* Current time */
 select now();  -- returns timestamp

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

あなたの場合:

 select to_timestamp(epoch_ms / 1000)::date;

PostgreSQL ドキュメント

おすすめ記事