Re: Order By Date Question

From: Adrian Klaver <aklaver(at)comcast(dot)net>
To: pgsql-general(at)postgresql(dot)org
Cc: BlackMage <dsd7872(at)uncw(dot)edu>
Subject: Re: Order By Date Question
Date: 2009-09-08 14:33:09
Message-ID: 200909080733.09819.aklaver@comcast.net
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

On Tuesday 08 September 2009 6:29:28 am BlackMage wrote:
> I have a question about ordering by date. I have a table with two fields
> and some date
>
> Name(character varying) | Event_Date(timestamp with timezone)
> A | 2009-09-10 5:30:00
> B | 2009-09-10- 00:00:00
> C | 2009-09-11 17:30:00
> D | 2009-09-11 07:30:00
>
>
> I want to order by date and then by name, so I want the result A,B,C,D. The
> problem is when I do a 'SELECT * FROM table_name ORDER BY Event_Date,
> DESC', it includes the actual time (HH:MM:SS) so the order comes out
> B,A,D,C.
>
> So what I am asking is how do I order only by the date? YYYY-MM-DD?
> --
> View this message in context:
> http://www.nabble.com/Order-By-Date-Question-tp25346259p25346259.html Sent
> from the PostgreSQL - general mailing list archive at Nabble.com.

Cast the timestamp to date:

SELECT * FROM table_name ORDER BY Event_Date::date DESC

Though to guarantee the order you want you will have to do:

SELECT * FROM table_name ORDER BY Event_Date::date, Name
--
Adrian Klaver
aklaver(at)comcast(dot)net

In response to

Browse pgsql-general by date

  From Date Subject
Next Message Martin Gainty 2009-09-08 14:41:37 Re: Adding integers ( > 8 bytes) to an inet
Previous Message Sam Mason 2009-09-08 14:32:47 Re: Order By Date Question