Re: getting last day of month

From: Chris Browne <cbbrowne(at)acm(dot)org>
To: pgsql-general(at)postgresql(dot)org
Subject: Re: getting last day of month
Date: 2005-08-25 21:23:21
Message-ID: 60r7chn22e.fsf@dba2.int.libertyrms.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

egy(at)tnet(dot)dp(dot)ua (Sergey Pariev) writes:
> I need to find out the last day of current month. Currently I do the
> trick with code below, but that's rather ugly way to do it IMHO. Could
> anybody suggest me a better way ?

log_analysis=# select date_trunc('months', (date_trunc('months', now()) + '45 days'::interval)) - '1 day'::interval;
?column?
------------------------
2005-08-31 00:00:00+00
(1 row)

So...

log_analysis=# create or replace function eom (timestamptz) returns timestamptz as '
log_analysis'# select date_trunc(''months'', (date_trunc(''months'', $1) + ''45 days''::interval)) - ''1 day''::interval;' language sql;
CREATE FUNCTION
log_analysis=# select eom(now());
eom
------------------------
2005-08-31 00:00:00+00
(1 row)
log_analysis=# select eom('2004-07-02');
eom
------------------------
2004-07-31 00:00:00+00
(1 row)

log_analysis=# select eom('2004-02-29');
eom
------------------------
2004-02-29 00:00:00+00
(1 row)
log_analysis=# select eom('2004-02-29'), eom('2005-02-28'), eom('2005-03-01');
eom | eom | eom
------------------------+------------------------+------------------------
2004-02-29 00:00:00+00 | 2005-02-28 00:00:00+00 | 2005-03-31 00:00:00+00
(1 row)
--
let name="cbbrowne" and tld="cbbrowne.com" in name ^ "@" ^ tld;;
http://www3.sympatico.ca/cbbrowne/emacs.html
Q: How many Newtons does it take to change a light bulb?
A: Faux! There to eat lemons, axe gravy soup!

In response to

Browse pgsql-general by date

  From Date Subject
Next Message Jeff Davis 2005-08-25 21:30:56 Re: Postgresql replication
Previous Message Chris Browne 2005-08-25 21:17:52 Re: Postgresql replication