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 0BCC3D1B9A9 for ; Thu, 26 Feb 2004 14:46:05 +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 46198-06 for ; Thu, 26 Feb 2004 10:46:04 -0400 (AST) Received: from tomts13-srv.bellnexxia.net (tomts13-srv.bellnexxia.net [209.226.175.34]) by svr1.postgresql.org (Postfix) with ESMTP id C1F6CD1B994 for ; Thu, 26 Feb 2004 10:45:59 -0400 (AST) Received: from acer.sympatico.ca ([65.93.65.138]) by tomts13-srv.bellnexxia.net (InterMail vM.5.01.06.05 201-253-122-130-105-20030824) with ESMTP id <20040226144558.DTHM25409.tomts13-srv.bellnexxia.net@acer.sympatico.ca> for ; Thu, 26 Feb 2004 09:45:58 -0500 Message-Id: <5.2.1.1.0.20040226093422.029e0560@pop6.sympatico.ca> X-Sender: b1wudu74@pop6.sympatico.ca X-Mailer: QUALCOMM Windows Eudora Version 5.2.1 Date: Thu, 26 Feb 2004 09:45:05 -0500 To: pgsql-sql@postgresql.org From: Frank Bax Subject: Re: Last day of month In-Reply-To: <403D7676.6080306@joeconway.com> References: <542c7f6d5ebe4be44d6b3778850f38ff@biglumber.com> <542c7f6d5ebe4be44d6b3778850f38ff@biglumber.com> Mime-Version: 1.0 Content-Type: text/plain; charset="us-ascii"; format=flowed X-Virus-Scanned: by amavisd-new at postgresql.org X-Archive-Number: 200402/364 X-Sequence-Number: 17054 At 11:30 PM 2/25/04, Joe Conway wrote: >Greg Sabino Mullane wrote: >>>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. > >oops...forget my last reply...I was a bit too quick on the draw. Try this >instead: > >regression=# select date_trunc('month', current_date + '1 >month'::interval) - '1 day'::interval; > ?column? >--------------------- > 2004-02-29 00:00:00 >(1 row) > >Joe But the original request was for a specific day-of-week. So use Joe's answer above to get last day of month, and use 'dow' to determine the day-of-week of that day. Let's call that dow1. If the day-of-week being asked for is dow2 then: if dow1 < dow2 return (last-day-of-month - dow1 - 7 + dow2) else return (last-day-of-month - dow1 + dow2) I'm no good at coding pgsql functions, so I'm not going to attempt proper syntax. Frank