Re: ORDER BY Problem

From: teg(at)redhat(dot)com (Trond Eivind =?iso-8859-1?q?Glomsr=F8d?=)
To: S(dot)Olloz(at)soid(dot)ch
Cc: pgsql-general(at)postgresql(dot)org
Subject: Re: ORDER BY Problem
Date: 2001-06-06 03:03:12
Message-ID: xuy4rtu1f9r.fsf@halden.devel.redhat.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

Severin Olloz <S(dot)Olloz(at)soid(dot)ch> writes:

> Why does Postgresql order the uppercase letters first?

That is locale dependent - if you don't use a proper locale but use
straight ASCII, A...Za...d would be correct. If you set a locale,
you'll get (Aa)(Bb) etc instead, which is the correct way to sort in
human languages.

> I have e.g. a table with one row an in this row there are follow values:
>
> row1
> ----
> ADC
> aa
> ABC
>
> With this select-syntax
>
> select * from table order by row1
>
> I become this output
>
> ABC
> ADC
> aa

Note that "aa" can give you some surprises anyway... e.g. in
Norwegian, "aa" is mapped to "å", the last character in the alphabet
(in Norwegian words, not foreign names etc... this distinction is
obviously rather hard to sort by for computers:).

Note that not using locales also makes "ORDER BY" give other bogus
results - as ASCII (or latin1) doesn't know the local alphabets, it
can't sort according to them. Example, for Norwegian:

create table bar(
ord varchar(40),
foo int,
primary key(ord));

insert into bar values('ære',2);
insert into bar values('åre',3);
insert into bar values('are',4);
insert into bar values('zsh',5);
insert into bar values('begynne',6);
insert into bar values('øve',7);

select ord,foo from bar order by ord;

Here is a valid result:

are | 4
begynne | 6
zsh | 5
ære | 2
øve | 7
åre | 3

Here is an invalid result:

are | 4
begynne | 6
zsh | 5
åre | 3
ære | 2
øve | 7

The invalid result is what you'll get if you run postgresql without
locale information, so make sure you set one before you initialize the
database and run the postmaster.
--
Trond Eivind Glomsrød
Red Hat, Inc.

In response to

Browse pgsql-general by date

  From Date Subject
Next Message Doug McNaught 2001-06-06 03:14:06 Re: Sequences in transaction context
Previous Message Eric G. Miller 2001-06-06 02:57:50 Re: Sequences in transaction context