pg.ddx.io  pgsql-sql@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Greg Sabino Mullane <greg@turnstep.com>
To: pgsql-sql@postgresql.org
Cc: sgnerd@yahoo.com.sg
Subject: Re: Last day of month
Date: Thu, 26 Feb 2004 03:07:52 -0000
Message-ID: <542c7f6d5ebe4be44d6b3778850f38ff@biglumber.com> (raw)
In-Reply-To: <050b01c3fb9b$de4e1e30$7502a8c0@hdsc.com>


-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1
 
 
> How to find the last sunday/mon..../sat of any given month.
 
There is probably a smoother way to do it, but here is a
quick little function to do what you ask. Feed it a date
and a number, where 0 is Sunday, 1 is Monday, etc.
 
CREATE OR REPLACE FUNCTION lastday(date,int) RETURNS DATE AS '
DECLARE
  match date;
  tomorrow date;
BEGIN
 
SELECT TO_DATE(
 (SELECT EXTRACT(\'year\' FROM $1) || \' \' ||
         EXTRACT(\'month\' FROM $1) || \' 01\'),
  \'YYYY MM DD\') INTO tomorrow;
 
LOOP
  tomorrow := tomorrow + \'24 hours\'::interval;
  IF (1 = EXTRACT(\'day\' FROM tomorrow) ) THEN
    RETURN match;
  END IF;
  IF ($2 = EXTRACT(\'dow\' FROM tomorrow)) THEN
    match := tomorrow;
  END IF;
END LOOP;
END;
' LANGUAGE plpgsql;
 
- --
Greg Sabino Mullane greg@turnstep.com
PGP Key: 0x14964AC8 200402252206
                                                                                                                             
-----BEGIN PGP SIGNATURE-----
 
iD8DBQFAPWL1vJuQZxSWSsgRAu0tAKDO7oKbxOmfDpCUYpeDSwCwyALs7QCgvKT3
x+aqhBqzm9F87ESbsMe6HdQ=
=AriI
-----END PGP SIGNATURE-----





view thread (6+ messages)  latest in thread

Message-ID: <542c7f6d5ebe4be44d6b3778850f38ff@biglumber.com>
Permalink:  ../542c7f6d5ebe4be44d6b3778850f38ff@biglumber.com/
Also on:    postgresql.org/message-id/542c7f6d5ebe4be44d6b3778850f38ff@biglumber.com

 · 

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: greg@turnstep.com, sgnerd@yahoo.com.sg
  Subject: Re: Last day of month
  In-Reply-To: <542c7f6d5ebe4be44d6b3778850f38ff@biglumber.com>

* 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