Re: Ordering problem with varchar (DESC) - from general ml. - Mailing list pgsql-admin

From Tom Lane
Subject Re: Ordering problem with varchar (DESC) - from general ml.
Date
Msg-id 27019.1170261851@sss.pgh.pa.us
Whole thread Raw
In response to Ordering problem with varchar (DESC) - from general ml.  ("Alexandre Leclerc" <alexandre.leclerc@gmail.com>)
List pgsql-admin
"Alexandre Leclerc" <alexandre.leclerc@gmail.com> writes:
> We have a column (varchar) that has plain text time and it is indexed.
> When I do a query with the index, all the data is in the right order,
> but when I user ORDER BY .. DESC, the order is messed up. Example:

> By index 1: (date, time, data)
> SELECT * from t1;

What makes you think that query is using an index?  It's probably just
returning the data in physical order, which might look correctly ordered
if you inserted all the data in time order to start with.

> SELECT * from t1 ORDER BY date, time DESC;

Perhaps you mean "ORDER BY date DESC, time DESC" ?

I don't actually see how a varchar field set up like that would sort
in a sensible numeric order at all --- text comparisons are unlikely
to do what you'd like with the optional leading one.  Consider replacing
the varchar with a field of type TIME.

            regards, tom lane

pgsql-admin by date:

Previous
From: "Alexandre Leclerc"
Date:
Subject: Ordering problem with varchar (DESC) - from general ml.
Next
From: Sidar López Cruz
Date:
Subject: Role privileges