Thread: Sort order with spaces?

Sort order with spaces?

From
Kristian Jörg
Date:
Hello!

I am having troubles with sort order in Postgres. It seems that space is 
not handled at all?
For instance the following rows are sorted in MS SQL Server as:

LUNDGREN
M L R
MACDOWELL
MUSCLE

But in Postgres I get this order:

LUNDGREN
MACDOWELL
M L R
MUSCLE

I seems that space is no considered at all so "M L R" is interpreted as 
"MLR" i Postgres. This is a problem for me taht I need to resolve. How 
do I do that?

Regards
Kristian

--           \\|//          (@ @)
+------ooO--(_)--Ooo----------+------------------------------+
| Kristian Jörg               | Phone: +46 54 153395         |
| Devo IT AB                  | Fax:   +46 54 153389         |
| Box 533, SE-651 12 KARLSTAD | mailto:kristian.jorg@devo.se |
| Sweden                      | http://www.devo.se           |
+-----------------------------+------------------------------+



Re: Sort order with spaces?

From
Richard Huxton
Date:
On Wednesday 17 September 2003 10:47, Kristian Jörg wrote:
> Hello!
>
> I am having troubles with sort order in Postgres. It seems that space is
> not handled at all?
> For instance the following rows are sorted in MS SQL Server as:
>
> LUNDGREN
> M L R
> MACDOWELL
> MUSCLE
>
> But in Postgres I get this order:
>
> LUNDGREN
> MACDOWELL
> M L R
> MUSCLE

Sort order depends upon your locale settings (specifically LC_COLLATE), which
will have been set when you ran "initdb". Basically, sort orders for C ,
en_GB and fr will all be different. I'm guessing you expect "C" style
sorting.

Check the end of your postgresql.conf file to see what settings you currently
have.
See the manuals (Localization section) and list archives for plenty of
details.
--  Richard Huxton Archonet Ltd


Re: Sort order with spaces?

From
Kristian Jörg
Date:
<br /><br /> Richard Huxton wrote:<br /><blockquote cite="mid200309171144.02871.dev@archonet.com" type="cite"><pre
wrap="">OnWednesday 17 September 2003 10:47, Kristian Jörg wrote: </pre><blockquote type="cite"><pre wrap="">Hello!
 

I am having troubles with sort order in Postgres. It seems that space is
not handled at all?
For instance the following rows are sorted in MS SQL Server as:

LUNDGREN
M L R
MACDOWELL
MUSCLE

But in Postgres I get this order:

LUNDGREN
MACDOWELL
M L R
MUSCLE   </pre></blockquote><pre wrap="">
Sort order depends upon your locale settings (specifically LC_COLLATE), which 
will have been set when you ran "initdb". Basically, sort orders for C , 
en_GB and fr will all be different. I'm guessing you expect "C" style 
sorting.

Check the end of your postgresql.conf file to see what settings you currently 
have.
See the manuals (Localization section) and list archives for plenty of 
details. </pre></blockquote><br /> Hi Richard!<br /><br /> Unfortunately this does not seem to help! I dumped the
database,recreated the cluster with initdb with LC_COLLATE = C (and even tried setting LC_CTYPE to C also), and
restoredthe database. The same ordering appears...<br /> I did look through all the manuals and I noted that the method
forsetting locale is different for 7.2 and 7.3, so I followed the 7.2 docs. My postgres version is 7.2. <br /><br />
Thetable keeps the data above in a column named "NORMTEXT" and my SQL statement for the result above is:<br /> select *
frommytable where NORMTEXT >=  'LU'<br /> Nothing out of the ordinary there..<br /> Any help on this matter is
highlyappreciated! <br /><br /> Regards<br /> Kristian<br /><pre class="moz-signature" cols="72">--           \\|//
    (@ @)
 
+------ooO--(_)--Ooo----------+------------------------------+
| Kristian Jörg               | Phone: +46 54 153395         |
| Devo IT AB                  | Fax:   +46 54 153389         |
| Box 533, SE-651 12 KARLSTAD | <a class="moz-txt-link-freetext"
href="mailto:kristian.jorg@devo.se">mailto:kristian.jorg@devo.se</a>|
 
| Sweden                      | <a class="moz-txt-link-freetext" href="http://www.devo.se">http://www.devo.se</a>
   |
 
+-----------------------------+------------------------------+</pre>

Re: Sort order with spaces?

From
Kristian Jörg
Date:
<br /><br /> Kristian Jörg wrote:<br /><blockquote cite="mid3F6853E4.3000604@devo.se" type="cite"></blockquote><br
/><br/> Richard Huxton wrote:<br /><blockquote cite="mid200309171144.02871.dev@archonet.com" type="cite"><pre
wrap="">OnWednesday 17 September 2003 10:47, Kristian Jörg wrote: </pre><blockquote type="cite"><pre wrap="">Hello!
 

I am having troubles with sort order in Postgres. It seems that space is
not handled at all?
For instance the following rows are sorted in MS SQL Server as:

LUNDGREN
M L R
MACDOWELL
MUSCLE

But in Postgres I get this order:

LUNDGREN
MACDOWELL
M L R
MUSCLE   </pre></blockquote><pre wrap="">
Sort order depends upon your locale settings (specifically LC_COLLATE), which 
will have been set when you ran "initdb". Basically, sort orders for C , 
en_GB and fr will all be different. I'm guessing you expect "C" style 
sorting.

Check the end of your postgresql.conf file to see what settings you currently 
have.
See the manuals (Localization section) and list archives for plenty of 
details. </pre></blockquote><br /> Hi Richard!<br /><br /> Unfortunately this does not seem to help! I dumped the
database,recreated the cluster with initdb with LC_COLLATE = C (and even tried setting LC_CTYPE to C also), and
restoredthe database. The same ordering appears...<br /> I did look through all the manuals and I noted that the method
forsetting locale is different for 7.2 and 7.3, so I followed the 7.2 docs. My postgres version is 7.2. <br /><br />
Thetable keeps the data above in a column named "NORMTEXT" and my SQL statement for the result above is:<br /> select *
frommytable where NORMTEXT >=  'LU'<br /> Nothing out of the ordinary there..<br /> Any help on this matter is
highlyappreciated! <br /><br /> Woops, a type above. My sql is of course:<br /> select * from mytable where
normtext>= 'LU' order by normtext;<br /><br /><blockquote cite="mid3F6853E4.3000604@devo.se" type="cite"><br />
Regards<br/> Kristian<br /><pre class="moz-signature" cols="72">--           \\|//          (@ @)
 
+------ooO--(_)--Ooo----------+------------------------------+
| Kristian Jörg               | Phone: +46 54 153395         |
| Devo IT AB                  | Fax:   +46 54 153389         |
| Box 533, SE-651 12 KARLSTAD | <a class="moz-txt-link-freetext"
href="mailto:kristian.jorg@devo.se">mailto:kristian.jorg@devo.se</a>|
 
| Sweden                      | <a class="moz-txt-link-freetext" href="http://www.devo.se">http://www.devo.se</a>
   |
 
+-----------------------------+------------------------------+</pre></blockquote><br /><pre class="moz-signature"
cols="72">--          \\|//          (@ @)
 
+------ooO--(_)--Ooo----------+------------------------------+
| Kristian Jörg               | Phone: +46 54 153395         |
| Devo IT AB                  | Fax:   +46 54 153389         |
| Box 533, SE-651 12 KARLSTAD | <a class="moz-txt-link-freetext"
href="mailto:kristian.jorg@devo.se">mailto:kristian.jorg@devo.se</a>|
 
| Sweden                      | <a class="moz-txt-link-freetext" href="http://www.devo.se">http://www.devo.se</a>
   |
 
+-----------------------------+------------------------------+</pre>

Re: Sort order with spaces?

From
Richard Huxton
Date:
On Wednesday 17 September 2003 13:39, Kristian Jörg wrote:
> Kristian Jörg wrote:
> > Richard Huxton wrote:
> >>>
> >>>LUNDGREN
> >>>M L R
> >>>MACDOWELL
> >>>MUSCLE
> >>>
> >>>But in Postgres I get this order:
> >>>
> >>>LUNDGREN
> >>>MACDOWELL
> >>>M L R
> >>>MUSCLE
> >>

> Woops, a type above. My sql is of course:
> select * from mytable where normtext>=  'LU' order by normtext;

richtest=# select * from foo order by b;a |     b
---+-----------1 | LUNDGREN2 | M L R3 | MACDOWELL4 | MUSCLE
(4 rows)

LC_MESSAGES = 'C'
LC_MONETARY = 'C'
LC_NUMERIC = 'C'
LC_TIME = 'C'

Might be worth checking the release notes on the website to see if there's
anything mentioned.

--  Richard Huxton Archonet Ltd


Re: Sort order with spaces?

From
Tom Lane
Date:
Kristian Jörg <krjg@devo.se> writes:
> Unfortunately this does not seem to help! I dumped the database, 
> recreated the cluster with initdb with LC_COLLATE = C (and even tried 
> setting LC_CTYPE to C also), and restored the database. The same 
> ordering appears...

You didn't do it right then ...

7.2's initdb is not helpful about telling you exactly what locale
settings it's using, but you could use the contrib/pg_controldata
utility to check what LC_COLLATE and LC_CTYPE settings got used.
(If you don't want to build pg_controldata, "strings
$PGDATA/global/pg_control" will do as a rough-and-ready substitute.)

Theoretically it should work to doexport LC_COLLATE=Cexport LC_CTYPE=Cinitdb
but if you have LANG or other LC_xxx values in your environment,
it's possible that there is some conflict.
        regards, tom lane


Re: Sort order with spaces?

From
Kristian Jörg
Date:
<br /><br /> Tom Lane wrote:<br /><blockquote cite="mid1935.1063807618@sss.pgh.pa.us" type="cite"><pre wrap="">Kristian
Jörg<a class="moz-txt-link-rfc2396E" href="mailto:krjg@devo.se"><krjg@devo.se></a> writes: </pre><blockquote
type="cite"><prewrap="">Unfortunately this does not seem to help! I dumped the database, 
 
recreated the cluster with initdb with LC_COLLATE = C (and even tried 
setting LC_CTYPE to C also), and restored the database. The same 
ordering appears...   </pre></blockquote><pre wrap="">
You didn't do it right then ...

7.2's initdb is not helpful about telling you exactly what locale
settings it's using, but you could use the contrib/pg_controldata
utility to check what LC_COLLATE and LC_CTYPE settings got used.
(If you don't want to build pg_controldata, "strings
$PGDATA/global/pg_control" will do as a rough-and-ready substitute.) </pre></blockquote> Unfortunately I am using a
binarydistribution from a RPM package, so I don't have the source. Tried the strings stuff though and that worked. I
havesv_SE as locale for sure. So you are corect. I did not do initdb correctly. Shame on me... :-)<br /><blockquote
cite="mid1935.1063807618@sss.pgh.pa.us"type="cite"><pre wrap="">
 
Theoretically it should work to doexport LC_COLLATE=Cexport LC_CTYPE=Cinitdb
but if you have LANG or other LC_xxx values in your environment,
it's possible that there is some conflict.
        regards, tom lane</pre></blockquote> It is possible my locale settings did not get through to initdb since I
triedusing the startup postgres command in /etc/init.d (with som modifications of course). I will try again manually
thistime!<br /><br /> Regards Kristian<br /><blockquote cite="mid1935.1063807618@sss.pgh.pa.us" type="cite"><pre
wrap="">
.
 </pre></blockquote><br /><pre class="moz-signature" cols="72">--           \\|//          (@ @)
+------ooO--(_)--Ooo----------+------------------------------+
| Kristian Jörg               | Phone: +46 54 153395         |
| Devo IT AB                  | Fax:   +46 54 153389         |
| Box 533, SE-651 12 KARLSTAD | <a class="moz-txt-link-freetext"
href="mailto:kristian.jorg@devo.se">mailto:kristian.jorg@devo.se</a>|
 
| Sweden                      | <a class="moz-txt-link-freetext" href="http://www.devo.se">http://www.devo.se</a>
   |
 
+-----------------------------+------------------------------+</pre>

Re: Sort order with spaces?

From
Kristian Jörg
Date:
Tom and Richard,<br /><br /> I have successfully fixed this problem now thanks to your support! The setting of
LC_COLLATEto C prior to running initdb fixed all problems finally once I had sorted out my error in passing the
environmentvariables correctly.<br /><br /> Thanx!<br /> /Kristian<br /><br /> Kristian Jörg wrote:<br /><blockquote
cite="mid3F686F33.7000003@devo.se"type="cite"></blockquote><br /><br /> Tom Lane wrote:<br /><blockquote
cite="mid1935.1063807618@sss.pgh.pa.us"type="cite"><pre wrap="">Kristian Jörg <a class="moz-txt-link-rfc2396E"
href="mailto:krjg@devo.se"><krjg@devo.se></a>writes: </pre><blockquote type="cite"><pre wrap="">Unfortunately
thisdoes not seem to help! I dumped the database, 
 
recreated the cluster with initdb with LC_COLLATE = C (and even tried 
setting LC_CTYPE to C also), and restored the database. The same 
ordering appears...   </pre></blockquote><pre wrap="">
You didn't do it right then ...

7.2's initdb is not helpful about telling you exactly what locale
settings it's using, but you could use the contrib/pg_controldata
utility to check what LC_COLLATE and LC_CTYPE settings got used.
(If you don't want to build pg_controldata, "strings
$PGDATA/global/pg_control" will do as a rough-and-ready substitute.) </pre></blockquote> Unfortunately I am using a
binarydistribution from a RPM package, so I don't have the source. Tried the strings stuff though and that worked. I
havesv_SE as locale for sure. So you are corect. I did not do initdb correctly. Shame on me... :-)<br /><blockquote
cite="mid1935.1063807618@sss.pgh.pa.us"type="cite"><pre wrap="">Theoretically it should work to doexport
LC_COLLATE=CexportLC_CTYPE=Cinitdb
 
but if you have LANG or other LC_xxx values in your environment,
it's possible that there is some conflict.
        regards, tom lane</pre></blockquote> It is possible my locale settings did not get through to initdb since I
triedusing the startup postgres command in /etc/init.d (with som modifications of course). I will try again manually
thistime!<br /><br /> Regards Kristian<br /><blockquote cite="mid1935.1063807618@sss.pgh.pa.us" type="cite"><pre
wrap="">.
 </pre></blockquote><br /><pre class="moz-signature" cols="72">--           \\|//          (@ @)
+------ooO--(_)--Ooo----------+------------------------------+
| Kristian Jörg               | Phone: +46 54 153395         |
| Devo IT AB                  | Fax:   +46 54 153389         |
| Box 533, SE-651 12 KARLSTAD | <a class="moz-txt-link-freetext"
href="mailto:kristian.jorg@devo.se">mailto:kristian.jorg@devo.se</a>|
 
| Sweden                      | <a class="moz-txt-link-freetext" href="http://www.devo.se">http://www.devo.se</a>
   |
 
+-----------------------------+------------------------------+</pre><br /><pre class="moz-signature" cols="72">--
   \\|//          (@ @)
 
+------ooO--(_)--Ooo----------+------------------------------+
| Kristian Jörg               | Phone: +46 54 153395         |
| Devo IT AB                  | Fax:   +46 54 153389         |
| Box 533, SE-651 12 KARLSTAD | <a class="moz-txt-link-freetext"
href="mailto:kristian.jorg@devo.se">mailto:kristian.jorg@devo.se</a>|
 
| Sweden                      | <a class="moz-txt-link-freetext" href="http://www.devo.se">http://www.devo.se</a>
   |
 
+-----------------------------+------------------------------+</pre>