Is there a better way than this to get the start and end of a month?

From: David Stanaway <david(at)stanaway(dot)net>
To: pgsql-sql(at)postgresql(dot)org
Subject: Is there a better way than this to get the start and end of a month?
Date: 2002-09-22 05:32:49
Message-ID: 1032672769.21707.62.camel@ciderbox
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-sql

Here are the 2 functions I have at the moment. I was wondering if
someone had a better way?

CREATE OR REPLACE FUNCTION month_start (date)
RETURNS date
AS '
DECLARE
day ALIAS FOR $1;
BEGIN
RETURN day - (extract(''day'' FROM day)||'' days'')::interval +
''1 day''::interval;
END;
'
LANGUAGE 'plpgsql';

CREATE OR REPLACE FUNCTION month_end (date)
RETURNS date
AS '
DECLARE
day ALIAS FOR $1;
month int;
year int;
BEGIN
month := extract(''month'' FROM day);
year := extract(''year'' FROM day);
IF month = 12 THEN
month := 1;
year := year +1;
ELSE
month := month +1;
END IF;
RETURN (''01-''||month||''-''||year)::date -
''1 day''::interval;
END;
'
LANGUAGE 'plpgsql';

--
David Stanaway

Responses

Browse pgsql-sql by date

  From Date Subject
Next Message Bruce Momjian 2002-09-23 01:51:55 Re: Monitoring a Query
Previous Message Rajesh Kumar Mallah. 2002-09-21 04:45:09 Appending to an array[] feild...[ ltree ]