Received: from localhost (maia-3.hub.org [200.46.204.184]) by postgresql.org (Postfix) with ESMTP id ABC409FC0DF for ; Thu, 7 Jun 2007 15:22:14 -0300 (ADT) Received: from postgresql.org ([200.46.204.71]) by localhost (mx1.hub.org [200.46.204.184]) (amavisd-maia, port 10024) with ESMTP id 64170-02-8 for ; Thu, 7 Jun 2007 15:22:06 -0300 (ADT) X-Greylist: from auto-whitelisted by SQLgrey-1.7.5 Received: from mtao05.charter.net (mtao05.charter.net [209.225.8.179]) by postgresql.org (Postfix) with ESMTP id 4999C9FC110 for ; Thu, 7 Jun 2007 15:22:08 -0300 (ADT) Received: from aa09.charter.net ([10.20.200.161]) by mtao05.charter.net (InterMail vM.7.08.02.00 201-2186-121-20061213) with ESMTP id <20070607182207.RPLS1417.mtao05.charter.net@aa09.charter.net>; Thu, 7 Jun 2007 14:22:07 -0400 Received: from [10.0.1.3] (really [66.188.164.45]) by aa09.charter.net with ESMTP id <20070607182207.HFGK4382.aa09.charter.net@[10.0.1.3]>; Thu, 7 Jun 2007 14:22:07 -0400 In-Reply-To: References: <9938A77A-6E63-413D-8AC9-7CB741D075E6@seespotcode.net> Mime-Version: 1.0 (Apple Message framework v752.2) Content-Type: text/plain; charset=US-ASCII; delsp=yes; format=flowed Message-Id: Cc: Content-Transfer-Encoding: 7bit From: Michael Glaesemann Subject: Re: subtract a day from the NOW function Date: Thu, 7 Jun 2007 13:22:05 -0500 To: "Campbell, Lance" X-Mailer: Apple Mail (2.752.2) X-Chzlrs: 0 X-Virus-Scanned: Maia Mailguard 1.0.1 X-Archive-Number: 200706/64 X-Sequence-Number: 28554 [Please don't top-post. It makes the discussion difficult to follow.] On Jun 7, 2007, at 12:49 , Campbell, Lance wrote: > 1) I have a timestamp field, "some_timestamp", in table "some_table". > 2) I want to compare field "some_timestamp" to the current date - 1 > day. > I need to ignore hours, minutes and seconds. > > Possible options: > > A) SELECT * FROM some_table WHERE some_timestamp::date > > (CURRENT_DATE - > INTERVAL '1 day')::date Casting to date as you are will work. You can also use date_trunc: SELECT * FROM some_table WHERE date_trunc('day', some_timestamp) > date_trunc('day', (CURRENT_DATE - INTERVAL '1 day')); Note the differences in the results: SELECT CURRENT_TIMESTAMP, date_trunc('day', CURRENT_TIMESTAMP), CURRENT_TIMESTAMP::date, CURRENT_DATE; now | date_trunc | now | date -------------------------------+------------------------+------------ +------------ 2007-06-07 13:21:28.186958-05 | 2007-06-07 00:00:00-05 | 2007-06-07 | 2007-06-07 date_trunc will return a timestamp. > B) SELECT * FROM some_table WHERE to_char(some_timestamp, > 'YYYYMMDD') > > to_char((now() - interval '1 day'), 'YYYYMMDD'); I'd never use to_char to compare dates. The built-in comparison operators work just fine. Michael Glaesemann grzm seespotcode net