Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to calculate end of the month in Postgres?

Tags:

postgresql

How to calculate end of the month in Postgres? I have table with column date datatype. I want to calculate end of the month of every date. For Eg. In the table there values like "2015-07-10 17:52:51","2015-05-30 11:30:19" then end of the month should be like 31 July 2015,31 May 2015. Please guide me in this.

like image 368
user1041240 Avatar asked Dec 24 '22 17:12

user1041240


1 Answers

How about truncating to the beginning of this month, jumping forward one month, then back one day?

=#  select (date_trunc('month', now()) + interval '1 month - 1 day')::date;
date
------------
 2015-07-31
(1 row)

Change now() to your date variable, which must be a timestamp, per the docs. You can then manipulate this output (with strftime, etc.) to any format you need.

Source

like image 180
Jake Worth Avatar answered Jan 13 '23 14:01

Jake Worth