Re: date comparisons

From: "Belinda M(dot) Giardine" <giardine(at)bio(dot)cse(dot)psu(dot)edu>
To: Erik Jones <erik(at)myemma(dot)com>
Cc: pgsql-general(at)postgresql(dot)org
Subject: Re: date comparisons
Date: 2006-12-12 17:03:20
Message-ID: Pine.GSO.4.53.0612121157310.28737@galapagos.bx.psu.edu
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general pgsql-patches

On Tue, 12 Dec 2006, Erik Jones wrote:

> Belinda M. Giardine wrote:
> > This should be simple but I am missing something. I am trying to extract
> > all records entered after a given date. The table has a field
> > date_entered which is a timestamp. In this particular case I am not
> > worried about time.
> >
> > I have tried:
> > select id from main_table where
> > date_entered > to_timestamp('January 2006', 'Month YYYY');
> >
> > select id from main_table where
> > (to_timestamp('January 2006', 'Month YYYY'), now()) overlaps (date_entered, date_entered);
> >
> > Both of these return all the rows in the table. Half of the rows are
> > dated 2000-06-22 12:00:00.
> >
> > PostgreSQL version 8.1.4
> >
> I think people often make date comparisons too difficult in postgres.
>
> select id
> from main_table
> where date_entered >= '2006-01-01';
>
> There are built in conversions for formatted date strings.
>
> --
> erik jones <erik(at)myemma(dot)com>
> software development
> emma(r)
>

Thanks that works. But I am trying to understand why the others did not,
especially my first attempt. Further testing shows that

select id, date_entered from main_table where
date_entered >= to_timestamp('2006 January', 'YYYY Month');

works, but

select id, date_entered from main_table where
date_entered >= to_timestamp('January 2006', 'Month YYYY');

does not. The order of the fields in the to_timestamp function changes
the timestamp produced. Should it be this way?

hbvar=# select to_timestamp('January 2006', 'Month YYYY');
to_timestamp
------------------------
0006-01-01 00:00:00-05
(1 row)

hbvar=# select to_timestamp('2006 January', 'YYYY Month');
to_timestamp
------------------------
2006-01-01 00:00:00-05
(1 row)

Belinda

In response to

Responses

Browse pgsql-general by date

  From Date Subject
Next Message Ragnar 2006-12-12 17:13:47 Re: a question on SQL
Previous Message Marc Mamin 2006-12-12 16:50:53 Re: resetting sequence to cur max value

Browse pgsql-patches by date

  From Date Subject
Next Message Bruce Momjian 2006-12-12 17:15:06 Re: Load distributed checkpoint
Previous Message Erik Jones 2006-12-12 16:46:16 Re: date comparisons