473,669 Members | 2,452 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Sort order confusion

I am suffering some sort order confusion. Given a database, "foo",
with a single character(4) column of data left padded with spaces I
get:

select * from foo order by somechars;

somechars
-----------
0
1
10
100
1011
111
1512
2
222
3
333

The output I desire is a basic ASCII sort:

somechars
-----------
0
1
2
3
10
100
111
222
333
1011
1512

(This sample set just has spaces and numeric digits but could have
other characters - I want the output in ASCII sort order)

1. What is the correct way to do this?

2. How do I verify the locale setting of an existing database cluster
(to verify that I really initialized it as "initdb -d --locale=C -D
/var/lib/pgsql/data")?

3. Should I have included "--enable-locale" or similar option when I
built Postgresql (the build is vanilla 7.4.1 "./configure ; make ;
make install")?

Cheers,
Steve
---------------------------(end of broadcast)---------------------------
TIP 2: you can get off all lists at once with the unregister command
(send "unregister YourEmailAddres sHere" to ma*******@postg resql.org)

Nov 22 '05 #1
7 7525
On Fri, 2004-02-06 at 11:12, Steve Crawford wrote:
I am suffering some sort order confusion. Given a database, "foo",
with a single character(4) column of data left padded with spaces I
get:

select * from foo order by somechars;

somechars
-----------
0
1
10
100
1011
111
1512
2
222
3
333

The output I desire is a basic ASCII sort:

somechars
-----------
0
1
2
3
10
100
111
222
333
1011
1512

(This sample set just has spaces and numeric digits but could have
other characters - I want the output in ASCII sort order)


Your original sort is a basic lexigraphic ("alphabetic al" by ASCII
character set number) sort.

What you appear to want is a numeric sort, where the numbers come out in
the order of numbers, rather than in their ASCII character set order.

If there were just digits that would be fairly easy, but I can't see any
way (short of post-processing the list in other software or writing
yourself a stored procedure) to do it in SQL.

Stephen

-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.2.3 (GNU/Linux)

iD8DBQBAIt4V4hF S2REFUecRAkPYAJ 0dUnhZDthau4u3r lY2u/Zu6ndbMQCginci
zu1Wv3XR0KtNK85 NkonbciY=
=VAoe
-----END PGP SIGNATURE-----

Nov 22 '05 #2
On Thursday 05 February 2004 4:21 pm, Stephen Robert Norris wrote:
On Fri, 2004-02-06 at 11:12, Steve Crawford wrote:
I am suffering some sort order confusion. Given a database,
"foo", with a single character(4) column of data left padded with
spaces I get:

select * from foo order by somechars;

somechars
-----------
0
1
10
100
1011
111
1512
2
222
3
333
<snip>

Your original sort is a basic lexigraphic ("alphabetic al" by ASCII
character set number) sort.

What you appear to want is a numeric sort, where the numbers come
out in the order of numbers, rather than in their ASCII character
set order.


Not exactly. I _DO_ want it in ASCII character set order which
includes spaces (0x20) sorting ahead of digits (0x30 - 0x39). This is
not what is happening. The first sort is some SQL sort order that
seems to ignore certain characters. Note the different sort order if
I pad with 'x' instead of '<space>':

somechars
-----------
1011
1512
x100
x111
x222
x333
x444
x555
x666
x777
x888
xx10
xx44
xx55
xxx0
xxx1
xxx2
xxx3
xxx4
xxx5
xxx6
xxx7
xxx8
xxx9
xxxx

Naturally if I were dealing with fields guaranteed to have something
that would convert to an int I could just order by, say,
int4(somechars) but that is not the case.

I even tried the to_ascii function but apparently that's the wrong
approach:
ERROR: encoding conversion from SQL_ASCII to ASCII not supported

Cheers,
Steve
---------------------------(end of broadcast)---------------------------
TIP 1: subscribe and unsubscribe commands go to ma*******@postg resql.org

Nov 22 '05 #3
On Fri, Feb 06, 2004 at 11:21:41AM +1100, Stephen Robert Norris wrote:
On Fri, 2004-02-06 at 11:12, Steve Crawford wrote:
I am suffering some sort order confusion. Given a database, "foo",
with a single character(4) column of data left padded with spaces I
get:

select * from foo order by somechars;


Cast it:

alvherre=> select * from foo order by bar::text::int;
bar
------
0
1
10
33
100
101
333
503
(8 filas)

--
Alvaro Herrera (<alvherre[a]dcc.uchile.cl>)
"La naturaleza, tan frgil, tan expuesta a la muerte... y tan viva"

---------------------------(end of broadcast)---------------------------
TIP 8: explain analyze is your friend

Nov 22 '05 #4
On Thursday 05 February 2004 5:01 pm, Alvaro Herrera wrote:
On Fri, Feb 06, 2004 at 11:21:41AM +1100, Stephen Robert Norris

wrote:
On Fri, 2004-02-06 at 11:12, Steve Crawford wrote:
I am suffering some sort order confusion. Given a database,
"foo", with a single character(4) column of data left padded
with spaces I get:

select * from foo order by somechars;


Cast it:

alvherre=> select * from foo order by bar::text::int;
bar
------
0
1
10
33
100
101
333
503
(8 filas)


Can't. As noted in the original post the column may contain data that
won't convert to an int (all spaces, characters, punctuation). I'm
seeking generic true ASCII sort order.

Cheers,
Steve
---------------------------(end of broadcast)---------------------------
TIP 8: explain analyze is your friend

Nov 22 '05 #5
On Thu, 5 Feb 2004, Steve Crawford wrote:
2. How do I verify the locale setting of an existing database cluster
(to verify that I really initialized it as "initdb -d --locale=C -D
/var/lib/pgsql/data")?


I think it'd be
pg_controldata /var/lib/pgsql/data
---------------------------(end of broadcast)---------------------------
TIP 3: if posting/reading through Usenet, please send an appropriate
subscribe-nomail command to ma*******@postg resql.org so that your
message can get through to the mailing list cleanly

Nov 22 '05 #6
Steve Crawford <sc*******@pinp ointresearch.co m> writes:
Not exactly. I _DO_ want it in ASCII character set order which
includes spaces (0x20) sorting ahead of digits (0x30 - 0x39). This is
not what is happening. The first sort is some SQL sort order that
seems to ignore certain characters.


Sounds to me like you've got the database in a non-C locale. See past
discussions ...

regards, tom lane

---------------------------(end of broadcast)---------------------------
TIP 6: Have you searched our list archives?

http://archives.postgresql.org

Nov 22 '05 #7
On Thursday 05 February 2004 6:08 pm, Tom Lane wrote:
Steve Crawford <sc*******@pinp ointresearch.co m> writes:
Not exactly. I _DO_ want it in ASCII character set order which
includes spaces (0x20) sorting ahead of digits (0x30 - 0x39).
This is not what is happening. The first sort is some SQL sort
order that seems to ignore certain characters.


Sounds to me like you've got the database in a non-C locale. See
past discussions ...


That was my first inclination (as noted in the full version of my
original post - now lost to the thread) but my installation history
showed:
initdb -d --locale=C -D /var/lib/pgsql/data

Nonetheless pg_controldata shows:
LC_COLLATE: en_US
LC_CTYPE: en_US

I now suspect that currently active "real" database was in-fact
created not by my several test initializations but by the SuSE
startup script which will run an "initdb" if the database has not
been initialized and which reads /etc/sysconfig/language to get
default locale settings.

So...

Is there a method of changing the locale of an extant database or do I
need to dump ; delete db ; reinit ; restore ?

Cheers,
Steve
---------------------------(end of broadcast)---------------------------
TIP 5: Have you checked our extensive FAQ?

http://www.postgresql.org/docs/faqs/FAQ.html

Nov 22 '05 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
3744
by: its me | last post by:
Let's say I have a class of people... Public Class People Public Sex as String Public Age as int Public Name as string end class And I declare an array of this class...
6
2332
by: alexhong2001 | last post by:
Does "std::sort" work only with sequence containers, not associative containers at all? Among sequential containers, can it be used with "list", "queue" and other sequence containers besides "vector"? Are "istringstream" and "ostringstream" covered in the book, "STL Tutorial and Reference" (second edition) by D. Musser, et al.? Seems like I could not find any related topic in the book.
20
4055
by: Xah Lee | last post by:
Sort a List Xah Lee, 200510 In this page, we show how to sort a list in Python & Perl and also discuss some math of sort. To sort a list in Python, use the “sort” method. For example: li=;
11
3884
by: James P. | last post by:
Hello, I have a report with the Priority field is used as sort order and grouping. The problem is the data in this Priority field if sorted in ascending order is: High, Low, and Medium. How could I sort it as: Low, Medium, High? Any suggestion is greatly appreciated, James
5
1563
by: TM | last post by:
I am using an access database in my vb.net application and it is tied to a datagrid. My problem is that the field I want to sort on is a text field, 5 characters long, and it contains not only numbers but some fields are text. When I sort the table in access, or use the "order by" sql statement, it seems to want to put the numbers first, then the alpha after. I realize this is probably the proper behavior, but is there any way I can
6
3367
by: Julian | last post by:
Hi, I am a very beginner in databases. I created a database table in Access 2003 and OOo 2.03 that includes name, address, postcode, phone numbers etc of our customers. I would like to sort customers by areas of London, UK by the first part of the postcode. The London postcodes are the form of E19 4PR, NW5U 4RT. So I would like to print a report (customers arranged into Word tables - normal tables, not database tables) for example which...
2
1908
by: Randy | last post by:
I have two listboxes on a form. The first box displays categories while the second box displays the items belonging to the category selected in the first box. Thus, the second box is essentially display only. I'm trying to sort these lists alphabetically A->Z. I have the Sort property set to True. When I run the app, the items in each box are sorted correctly. So far, so good. What I am confused by is that when a category is...
3
5176
by: aRTx | last post by:
I have try a couple of time but does not work for me My files everytime are sortet by NAME. I want to Sort my files by Date-desc. Can anyone help me to do it? The Script <? /* ORIGJINALI
7
5100
by: otis | last post by:
Hi all, This is a small issue to make things prettier, but we all know how important that can be! I had an xsl:if to check if a node was the last one in a collection of nodes and if it was a horizontal divider would not be displayed underneath it. This worked a treat but now I'm trying to sort my data using xsl:sort. The if statement technically still works but now the node identified by last() could be anywhere in the output order. I only...
0
8465
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, well explore What is ONU, What Is Router, ONU & Routers main usage, and What is the difference between ONU and Router. Lets take a closer look ! Part I. Meaning of...
0
8383
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8809
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8588
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
7407
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development projectplanning, coding, testing, and deploymentwithout human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5682
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4206
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
2797
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
1788
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.