LoginSignup
0
0

More than 5 years have passed since last update.

Redshift で前月末尾を取得する

Posted at

日付操作

redshift(おそらく postgresql も同じ)で日付操作することってありますよね。前月の末尾ってどうやったらとれるんでしょうか。

結論

ずばりこれです。

dateadd(days, -1, date_trunc('month', current_date));

date_truncmonth を指定して日付情報を落として1日を表し、その日に対して dateadd-1 して1日引く。これで、前月の末尾を datetime 形式で取得できる。

さて使ってみましょう。例えば2017年1月19日現在下記を実行すると

redshift=# select dateadd(days, -1, date_trunc('month', current_date));

このように結果が返ってきます。

      date_add       
---------------------
 2016-12-31 00:00:00
(1 row)

どうぞ。

0
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
0
0