From: Bruce Momjian <pgman@candle.pha.pa.us>
To: aarni@kymi.com
Cc: pgsql-sql@postgresql.org
Subject: Re: Querying date_time for date only ?
Date: Thu, 22 Dec 2005 11:01:17 -0500 (EST)
Message-ID: <200512221601.jBMG1H807869@candle.pha.pa.us> (raw)
In-Reply-To: <200512201459.07358.aarni@kymi.com>
Aarni Ruuhimäki wrote:
> Hello List,
>
> I have a time stamp without time zone field, YYYY-MM-DD hh:mm:ss, in
> my table. I want to also find something just for a particular day
> regardless of the time.
>
> (Pg)SQL way to do this ?
Yes. You can use date_trunc():
test=> select date_trunc('day', '2004-01-04 04:02:03'::timestamp);
date_trunc
---------------------
2004-01-04 00:00:00
(1 row)
so it would be date_trunc('day', col) = '2004-01-05'. The problem with
this is that you cannot use an index unless you create an expression
index on the date_trunc() function call. Another option is to do
something like:
WHERE col >= '2004-01-04' AND col < '2004-01-05'
If the date isn't a constant, you have to use date_trunc() on those, and
add one day to the second comparison:
WHERE col >= date_trunc('day', col2) AND
col < date_trunc('day', col2) + '1 day';
--
Bruce Momjian | http://candle.pha.pa.us
pgman@candle.pha.pa.us | (610) 359-1001
+ If your life is a hard drive, | 13 Roberts Road
+ Christ can be your backup. | Newtown Square, Pennsylvania 19073
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Reply to all the recipients using the --to and --cc options:
reply via email
To: pgsql-sql@postgresql.org
Cc: pgman@candle.pha.pa.us, aarni@kymi.com
Subject: Re: Querying date_time for date only ?
In-Reply-To: <200512221601.jBMG1H807869@candle.pha.pa.us>
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox