X-Original-To: pgsql-sql-postgresql.org@localhost.postgresql.org Received: from localhost (unknown [200.46.204.2]) by svr1.postgresql.org (Postfix) with ESMTP id 9DC0AD1CA83 for ; Thu, 26 Feb 2004 16:36:19 +0000 (GMT) Received: from svr1.postgresql.org ([200.46.204.71]) by localhost (neptune.hub.org [200.46.204.2]) (amavisd-new, port 10024) with ESMTP id 89096-09 for ; Thu, 26 Feb 2004 12:36:12 -0400 (AST) Received: from mail.michaelchaney.com (mail.michaelchaney.com [207.65.84.68]) by svr1.postgresql.org (Postfix) with ESMTP id A59C0D1B91B for ; Thu, 26 Feb 2004 12:36:11 -0400 (AST) Received: from pcp08698233pcs.500ash01.tn.comcast.net (fractal.trailer) [69.137.114.17] by mail.michaelchaney.com with esmtp (Exim 4.20 #1 (FreeBSD)) id 1AwOQO-000FCI-3c; Thu, 26 Feb 2004 10:31:48 -0600 Received: from mdchaney by fractal.trailer with local (Exim 3.35 #1 (Debian)) id 1AwOVS-00061J-00; Thu, 26 Feb 2004 10:37:02 -0600 Date: Thu, 26 Feb 2004 10:37:02 -0600 From: Michael Chaney To: pgsql-sql@postgresql.org, sgnerd@yahoo.com.sg Subject: Re: Last day of month Message-ID: <20040226163702.GC22092@michaelchaney.com> References: <050b01c3fb9b$de4e1e30$7502a8c0@hdsc.com> <542c7f6d5ebe4be44d6b3778850f38ff@biglumber.com> Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <542c7f6d5ebe4be44d6b3778850f38ff@biglumber.com> User-Agent: Mutt/1.3.28i Organization: Michael Chaney Consulting Corporation X-URL: http://www.michaelchaney.com/ X-MailScanner-Information: Please see http://www.michaelchaney.com/mail/spam.php for more information X-MailScanner: Found to be clean X-MailScanner-SpamCheck: not spam, SpamAssassin (score=-3.4, required 5, IN_REP_TO) X-Virus-Scanned: by amavisd-new at postgresql.org X-Archive-Number: 200402/369 X-Sequence-Number: 17059 On Thu, Feb 26, 2004 at 03:07:52AM -0000, Greg Sabino Mullane wrote: > > -----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. select date_trunc('month', current_date + '1 month'::interval) - '1 day'::interval + (((3 - 7 - to_char(date_trunc('month', current_date + '1 month'::interval) - '1 day'::interval,'D')::int) %7)||' days')::interval; The "3" is the day of week (1 = Sunday, 7 = Saturday). This equation will return the date of the last "x" of the current month. Change "current_date" to be whatever date you wish to find the last "x" of. Michael -- Michael Darrin Chaney mdchaney@michaelchaney.com http://www.michaelchaney.com/