Received: from localhost (maia-2.hub.org [200.46.204.187]) by postgresql.org (Postfix) with ESMTP id 1208E9FB491 for ; Mon, 4 Jun 2007 18:08:43 -0300 (ADT) Received: from postgresql.org ([200.46.204.71]) by localhost (mx1.hub.org [200.46.204.187]) (amavisd-maia, port 10024) with ESMTP id 03510-03 for ; Mon, 4 Jun 2007 18:08:32 -0300 (ADT) X-Greylist: from auto-whitelisted by SQLgrey-1.7.5 Received: from smtp-out.neti.ee (smtp-out.neti.ee [194.126.126.41]) by postgresql.org (Postfix) with ESMTP id C72609FB391 for ; Mon, 4 Jun 2007 18:08:35 -0300 (ADT) Received: from localhost (localhost [127.0.0.1]) by MXR-5.estpak.ee (Postfix) with ESMTP id 004671CF59B; Tue, 5 Jun 2007 00:08:32 +0300 (EEST) X-Virus-Scanned: by amavisd-new-2.4.3 (20060930) (Debian) at neti.ee Received: from smtp-out.neti.ee ([127.0.0.1]) by localhost (MXR-2.estpak.ee [127.0.0.1]) (amavisd-new, port 10024) with ESMTP id pM4nKqD7STrc; Tue, 5 Jun 2007 00:08:30 +0300 (EEST) Received: from Relayhost2.neti.ee (Relayhost2 [88.196.174.142]) by MXR-5.estpak.ee (Postfix) with ESMTP id 159B21CFBC8; Tue, 5 Jun 2007 00:08:30 +0300 (EEST) In-Reply-To: <7B00D29F-089A-4A19-B9C1-0FF8A949DDA3@seespotcode.net> References: <46642A05.40708@joshuaneil.com> <20070604152736.GS32320@phlogiston.dyndns.org> <7B00D29F-089A-4A19-B9C1-0FF8A949DDA3@seespotcode.net> Mime-Version: 1.0 (Apple Message framework v752.3) Content-Type: text/plain; charset=US-ASCII; delsp=yes; format=flowed Message-Id: Cc: Andrew Sullivan , pgsql-sql@postgresql.org Content-Transfer-Encoding: 7bit From: Kristo Kaiv Subject: Re: current_date / datetime stuff Date: Tue, 5 Jun 2007 00:08:28 +0300 To: Michael Glaesemann X-Mailer: Apple Mail (2.752.3) X-Virus-Scanned: Maia Mailguard 1.0.1 X-Archive-Number: 200706/23 X-Sequence-Number: 28513 oneliner: select date_trunc('month',now()) + ((8 - extract('dow' from date_trunc ('month',now()))||'days')::text)::interval; Kristo On 04.06.2007, at 19:39, Michael Glaesemann wrote: > > On Jun 4, 2007, at 10:59 , Michael Glaesemann wrote: > >> >> On Jun 4, 2007, at 10:27 , Andrew Sullivan wrote: >> >>> On Mon, Jun 04, 2007 at 10:04:37AM -0500, Joshua wrote: >>>> that will return the date of the first Monday of the month? >>> >>> I guess you need to write a function to do this. I suppose you >>> could >>> do it by finding out what day of the week it is and what the date >>> is, >>> then counting backwards to the earliest possible Monday. >> >> As Andrew said, there's no built-in function to do this, but it's >> easy enough to write one. Here's a rough example (very lightly >> tested and probably overly complicated) > > And a little simpler: > > CREATE OR REPLACE FUNCTION first_dow_of_month(DATE, INTEGER) > RETURNS DATE > IMMUTABLE > LANGUAGE SQL AS $_$ > SELECT v_first_day_of_month + ( 7 + $2 - v_day_of_week) % 7 > AS first_dow_of_month > FROM ( > SELECT v_first_day_of_month > , extract('dow' from v_first_day_of_month)::integer > AS v_day_of_week > FROM (SELECT date_trunc('month', $1)::date) > AS mon(v_first_day_of_month)) as calc; > $_$; > > CREATE OR REPLACE FUNCTION first_monday(DATE) > RETURNS DATE > IMMUTABLE > LANGUAGE SQL AS $_$ > SELECT first_dow_of_month($1, 1); > $_$; > > Michael Glaesemann > grzm seespotcode net > > > > ---------------------------(end of > broadcast)--------------------------- > TIP 3: Have you checked our extensive FAQ? > > http://www.postgresql.org/docs/faq