X-Original-To: pgsql-sql-postgresql.org@localhost.postgresql.org Received: from localhost (neptune.hub.org [200.46.204.2]) by svr1.postgresql.org (Postfix) with ESMTP id C9FDCD1B4F4 for ; Wed, 10 Dec 2003 00:53:30 +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 52475-05 for ; Tue, 9 Dec 2003 20:53:02 -0400 (AST) Received: from anchor-post-34.mail.demon.net (anchor-post-34.mail.demon.net [194.217.242.92]) by svr1.postgresql.org (Postfix) with ESMTP id 6AC7ED1B487 for ; Tue, 9 Dec 2003 20:52:58 -0400 (AST) Received: from tmsl-adsl.demon.co.uk ([80.177.114.181] helo=bacon.tmsl.demon.co.uk) by anchor-post-34.mail.demon.net with esmtp (Exim 3.35 #1) id 1ATsb7-000FmI-0Y; Wed, 10 Dec 2003 00:53:01 +0000 Date: Wed, 10 Dec 2003 00:52:56 +0000 From: Paul Thomas To: jeff@dnuk.com Cc: "pgsql-sql @ postgresql . org" Subject: Re: How to specify the beginning of the month in Postgres SQL syntax? Message-ID: <20031210005256.C30762@bacon> References: Mime-Version: 1.0 Content-Type: text/plain; format=flowed; charset=ISO-8859-1 In-Reply-To: ; from jeff@dnuk.com on Sun, Dec 07, 2003 at 16:16:44 +0000 X-Mailer: Balsa 1.2.3 Lines: 45 X-Virus-Scanned: by amavisd-new at postgresql.org X-Archive-Number: 200312/78 X-Sequence-Number: 16244 On 07/12/2003 16:16 jeff@dnuk.com wrote: > Hello, > > I need to create a view in Postgres that has a where clause of the > date < beginning of month. > > i.e.: > SELECT supplier_number, Sum(amount) AS due > FROM purchase_orders > WHERE date < '2003-12-1' AND paid = 0 > GROUP BY supplier_number > ORDER BY supplier_number ASC > > > As you can see, I've specified the 1st of December this year as the > where clause. What I want is an SQL statement that automatically > generates the first of the month. How do I do this? I do shed-loads of these date-related queries and although it's feasible to write some SQL/UDF function to do what you're asking, in my experience it is better to process the date in your app and pass it across as a parameter. That way you could use the same piece of SQL to get, for example, data which is > month owing just by passing 2003-11-01 as the date. Probably what you need is to write a function which takes an arbitary date and returns the first date in that month/year. You _could_ write this as PostgreSQL User Defined Function but writing it as part of your app will give you a) greater flexibility as the function will be easily available to other parts of your application b) if your app language/dev environment has a source-level debugger, you will be able to benefit from it when debugging your function and c) someone trying to maintain your app in 4 years time will only need to know your application language, SQL and a possible (very!) few PostgreSQL-specific variations from the SQL language definition. HTH -- Paul Thomas +------------------------------+---------------------------------------------+ | Thomas Micro Systems Limited | Software Solutions for the Smaller Business | | Computer Consultants | http://www.thomas-micro-systems-ltd.co.uk | +------------------------------+---------------------------------------------+