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

PostgreSQLでLAST_DAY関数を自作する

Last updated at Posted at 2024-06-22

OracleにあるファンクションがPostgreSQLでは用意されていない場合があるので自作します。

自作したファンクション

CREATE OR REPLACE FUNCTION LAST_DAY(DATE)
returns DATE as $$
BEGIN
return DATE(date_trunc('month',$1) + '1month' + '-1 Day');
END
$$ Language plpgsql;
  1. date_trunc()で与えられた日付を月単位で切り捨てて月初を取得
  2. 切り捨てて得た月初に1か月足す
  3. 最後に1日分引き算する

例) 2024-06-22 → 2024-06-01 → 2024-07-01 → 2024-06-30

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

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?