X-Original-To: pgsql-sql-postgresql.org@localhost.postgresql.org Received: from localhost (unknown [200.46.204.144]) by svr1.postgresql.org (Postfix) with ESMTP id 97F738B9B6A for ; Mon, 21 Feb 2005 20:53:37 +0000 (GMT) Received: from svr1.postgresql.org ([200.46.204.71]) by localhost (av.hub.org [200.46.204.144]) (amavisd-new, port 10024) with ESMTP id 91471-08 for ; Mon, 21 Feb 2005 20:53:27 +0000 (GMT) Received: from anchor-post-30.mail.demon.net (anchor-post-30.mail.demon.net [194.217.242.88]) by svr1.postgresql.org (Postfix) with ESMTP id 906E68B9B69 for ; Mon, 21 Feb 2005 20:53:27 +0000 (GMT) Received: from mwynhau.demon.co.uk ([193.237.186.96] helo=mainbox.archonet.com) by anchor-post-30.mail.demon.net with esmtp (Exim 4.42) id 1D3KYY-000Md7-1j; Mon, 21 Feb 2005 20:53:27 +0000 Received: from [192.168.1.17] (client17.archonet.com [192.168.1.17]) by mainbox.archonet.com (Postfix) with ESMTP id C6401172BC; Mon, 21 Feb 2005 20:53:25 +0000 (GMT) Message-ID: <421A4A45.7090309@archonet.com> Date: Mon, 21 Feb 2005 20:53:25 +0000 From: Richard Huxton User-Agent: Mozilla Thunderbird 1.0 (X11/20041206) X-Accept-Language: en-us, en MIME-Version: 1.0 To: Brandon Metcalf Cc: pgsql-sql@postgresql.org Subject: Re: query for records based on date References: In-Reply-To: Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit X-Virus-Scanned: by amavisd-new at hub.org X-Spam-Status: No, hits=0.063 tagged_above=0 required=5 tests=AWL, FORGED_RCVD_HELO X-Spam-Level: X-Archive-Number: 200502/229 X-Sequence-Number: 20340 Brandon Metcalf wrote: > If I have a table with an attribute of type "timestamp without time > zone", what is the best way to query that table for records that are > older than, say, one week. > > I suppose I could get the current time in epoch seconds and the time of > the attribute in the same, and select those records where the difference > is greater than the number of seconds in a week. Is there something > easier like > > SELECT * FROM table WHERE attribute IS OLDER THAN one week; Look up "intervals" in the manuals - you can just subtract timestamps/dates. Probably also worth searching the mailing lists for "ago()" and "volatile" to learn about the issues with indexing. -- Richard Huxton Archonet Ltd