1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

【PostgreSQL】SELECTしてきた値をタイムゾーン補正を行い、指定したフォーマットで日付と時刻を変換する方法

Posted at

はじめに

DBからSELECTしてきた値をタイムゾーン補正を行い、指定したフォーマットで日付と時刻を変換する機会があったので、その方法をまとめておこうと思います。

PostgreSQLでは動作確認済みですが、他のDBでは確認していないのでその点よろしくお願いします。

9時間マイナスする場合

sample.sql
SELECT TO_CHAR(your_column_name::timestamp - INTERVAL '9 hours', 'YYYY-MM-DD"T"HH24:MI:SS') AS adjusted_date
FROM your_table_name;

9時間プラスする場合

sample.sql
SELECT TO_CHAR(your_column_name::timestamp + INTERVAL '9 hours', 'YYYY-MM-DD"T"HH24:MI:SS') AS adjusted_date
FROM your_table_name;

解説

  1. your_column_name::timestamp

値をtimestamp型に変換

  1. - INTERVAL '9 hours'

取得したタイムスタンプから9時間マイナスする
UTCから JSTへ変換したいときは、9時間プラスしてあげれば良いです。

1
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?