X-Original-To: pgsql-sql@postgresql.org Received: from localhost (unknown [64.117.224.193]) by developer.postgresql.org (Postfix) with ESMTP id E28799250A8 for ; Tue, 20 May 2003 12:57:37 -0400 (EDT) Received: from developer.postgresql.org ([64.117.224.193]) by localhost (developer.postgresql.org [64.117.224.193:10024]) (amavisd-new) with ESMTP id 23098-06 for ; Tue, 20 May 2003 12:57:30 -0400 (EDT) Received: from megazone.bigpanda.com (megazone.bigpanda.com [63.150.15.178]) by developer.postgresql.org (Postfix) with ESMTP id 9778E9250A4 for ; Tue, 20 May 2003 12:57:28 -0400 (EDT) Received: by megazone.bigpanda.com (Postfix, from userid 1001) id 17691D625; Tue, 20 May 2003 09:57:27 -0700 (PDT) Received: from localhost (localhost [127.0.0.1]) by megazone.bigpanda.com (Postfix) with ESMTP id 0D19C5C0C; Tue, 20 May 2003 09:57:27 -0700 (PDT) Date: Tue, 20 May 2003 09:57:26 -0700 (PDT) From: Stephan Szabo To: Jan Bodey Cc: Subject: Re: Date comparison question In-Reply-To: Message-ID: <20030520095059.Q68048-100000@megazone23.bigpanda.com> MIME-Version: 1.0 Content-Type: TEXT/PLAIN; charset=US-ASCII X-Virus-Scanned: by amavisd-new X-Archive-Number: 200305/392 X-Sequence-Number: 13557 On Tue, 20 May 2003, Jan Bodey wrote: > Hi > > I require a query that will see if a stored date is more than a certain > interval before the current date. Using an interval of 10 hours, the query > would be something like: > > SELECT * FROM utable > WHERE now() - utimestamp > interval '10 hours'; > > The problem I am having is that the interval is not fixed, and I would like > to be able to store it as a value in a database table. The unit of the > interval can, however, be fixed, and will almost certainly be fixed to > hours. > > I have not been able to find a way of doing what I require, does anybody > have any ideas to what I need to do? If you assume that you're just going to stick one row in the interval table, I think you can do something kinda like: create table interv(i interval); insert into interv values ('10 hours'); analyze interv; select utable.* from utable, interv where now()-utimestamp>i; This could be made more complicated if you want to have multiple interval values, etc. Also, neither of the above is probably terribly fast if utable is big.