Skip site navigation (1) Skip section navigation (2)

Re: Last day of month

From: "Greg Sabino Mullane" <greg(at)turnstep(dot)com>
To: pgsql-sql(at)postgresql(dot)org
Cc: sgnerd(at)yahoo(dot)com(dot)sg
Subject: Re: Last day of month
Date: 2004-02-26 03:07:52
Message-ID: 542c7f6d5ebe4be44d6b3778850f38ff@biglumber.com (view raw or flat)
Thread:
Lists: pgsql-sql
-----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(at)turnstep(dot)com
PGP Key: 0x14964AC8 200402252206
                                                                                                                             
-----BEGIN PGP SIGNATURE-----
 
iD8DBQFAPWL1vJuQZxSWSsgRAu0tAKDO7oKbxOmfDpCUYpeDSwCwyALs7QCgvKT3
x+aqhBqzm9F87ESbsMe6HdQ=
=AriI
-----END PGP SIGNATURE-----



In response to

Responses

pgsql-sql by date

Next:From: Joe ConwayDate: 2004-02-26 04:27:57
Subject: Re: Last day of month
Previous:From: Kenneth GonsalvesDate: 2004-02-26 02:54:55
Subject: updating remote database

Privacy Policy | About PostgreSQL
Copyright © 1996-2014 The PostgreSQL Global Development Group