Re: getting last day of month - Mailing list pgsql-general

From Chris Browne
Subject Re: getting last day of month
Date
Msg-id 60r7chn22e.fsf@dba2.int.libertyrms.com
Whole thread Raw
In response to getting last day of month  (Sergey Pariev <egy@tnet.dp.ua>)
List pgsql-general
egy@tnet.dp.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!

pgsql-general by date:

Previous
From: Ralph Mason
Date:
Subject: Odd Problems
Next
From: Chris Browne
Date:
Subject: Re: Postgresql replication