Received: from localhost (maia-1.hub.org [200.46.204.191]) by postgresql.org (Postfix) with ESMTP id 679429FB524 for ; Tue, 15 May 2007 06:09:25 -0300 (ADT) Received: from postgresql.org ([200.46.204.71]) by localhost (mx1.hub.org [200.46.204.191]) (amavisd-maia, port 10024) with ESMTP id 70291-02 for ; Tue, 15 May 2007 06:09:21 -0300 (ADT) X-Greylist: from auto-whitelisted by SQLgrey-1.7.5 Received: from mail.metronet.co.uk (mail.metronet.co.uk [213.162.97.75]) by postgresql.org (Postfix) with ESMTP id B26B09FB550 for ; Tue, 15 May 2007 06:09:21 -0300 (ADT) Received: from server3.office.archonet.com (84-51-143-99.archon037.adsl.metronet.co.uk [84.51.143.99]) by smtp.metronet.co.uk (MetroNet Mail) with ESMTP id 42D7474E03; Tue, 15 May 2007 10:09:20 +0100 (BST) Received: from [192.168.1.36] (dell36.office.archonet.com [192.168.1.36]) by server3.office.archonet.com (Postfix) with ESMTP id 32C3327411A; Tue, 15 May 2007 10:09:20 +0100 (BST) Message-ID: <464978C0.2050004@archonet.com> Date: Tue, 15 May 2007 10:09:20 +0100 From: Richard Huxton User-Agent: Thunderbird 2.0.0.0 (X11/20070326) MIME-Version: 1.0 To: Gary Stainburn Cc: pgsql-sql@postgresql.org Subject: Re: workday function References: <200705150951.34928.gary.stainburn@ringways.co.uk> In-Reply-To: <200705150951.34928.gary.stainburn@ringways.co.uk> Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit X-Virus-Scanned: Maia Mailguard 1.0.1 X-Archive-Number: 200705/123 X-Sequence-Number: 28403 Gary Stainburn wrote: > Hi folks > > I need to be able to add and subtract workdays, something like > > select CURRENT_DATE - '3 work days'::interval; > > I can't see how to do this natively so I'm looking to write a function to do > it and was wondering if anyone's already done it. Don't know of one - not sure what "workday" would mean in a global sense. I mean, Mon-Fri in most European office settings, but you'd include Sat in retail settings and in Islamic countries presumably exclude Fridays. Our local library shuts early on Mondays iirc but is open Saturday mornings. Casting to interval won't work because work-days will be a variable amount of real-days based on what you're adding/subtracting from. > While Googling I've found that MS Excel has a workday function which seems to > do what I want. > > Any help would be appreciated. Well, you'll be wanting to use extract('dow' from current_date) or similar to figure out how many days to skip. There are national-holiday resources online, but I'm not sure if they take into account e.g. the extra day civil servants get in the UK (or used to) for the Queen's official birthday. -- Richard Huxton Archonet Ltd