pg.ddx.io  pgsql-sql@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: David Stanaway <david@stanaway.net>
To: pgsql-sql@postgresql.org
Subject: Is there a better way than this to get the start and end of a month?
Date: 22 Sep 2002 00:32:49 -0500
Message-ID: <1032672769.21707.62.camel@ciderbox> (raw)

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



view thread (3+ messages)  latest in thread

Message-ID: <1032672769.21707.62.camel@ciderbox>
Permalink:  ../1032672769.21707.62.camel@ciderbox/
Also on:    postgresql.org/message-id/1032672769.21707.62.camel@ciderbox

 · 

reply

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Reply to all the recipients using the --to and --cc options:
  reply via email

  To: pgsql-sql@postgresql.org
  Cc: david@stanaway.net
  Subject: Re: Is there a better way than this to get the start and end of a month?
  In-Reply-To: <1032672769.21707.62.camel@ciderbox>

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox