From: | Michael Glaesemann <grzm(at)seespotcode(dot)net> |
---|---|
To: | "Campbell, Lance" <lance(at)uiuc(dot)edu> |
Cc: | <pgsql-sql(at)postgresql(dot)org> |
Subject: | Re: subtract a day from the NOW function |
Date: | 2007-06-07 18:22:05 |
Message-ID: | D005E810-0FC2-42EC-98D6-5CA2B9B7B20C@seespotcode.net |
Views: | Raw Message | Whole Thread | Download mbox | Resend email |
Thread: | |
Lists: | pgsql-general pgsql-sql |
[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
From | Date | Subject | |
---|---|---|---|
Next Message | brian | 2007-06-07 18:42:06 | Re: querying the age of a row |
Previous Message | Lonni J Friedman | 2007-06-07 18:21:12 | Re: querying the age of a row |
From | Date | Subject | |
---|---|---|---|
Next Message | Steve Crawford | 2007-06-07 18:58:38 | Re: subtract a day from the NOW function |
Previous Message | Scott Marlowe | 2007-06-07 18:12:00 | Re: subtract a day from the NOW function |