[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: sorting invoice numbers by numerical order



On Tue, 16 Feb 2010, Chris Travers wrote:

On Mon, Feb 15, 2010 at 9:58 PM, Bob Miller <..hidden..> wrote:
There was some chat a while ago with regards to sorting by description,
and the two suggestions were to sort the query in the sql statement
before producing the html page, and the other was to use javascripting
to do the same.  My question is about numbers, though, not letters.
For my installations, when sorting by invoice number, my numbers go like
this:

1
10
100
101
102
103
104
105
106
107
108
109
11
110
111

and so on.  Is there a way to make it so invoices 11-99 comes *before*
100??

If all invoice numbers include only numbers instead of letters, you
could alter the SQL query to order by the invoice number converted to
a numeric datatype.  The problem though is that invoice numbers can
include letters, so consequently they are sorted in the db as text
strings.  Otherwise an invoice like 110-V would cause such a query to
error.

Does psql have anything along the lines of the MySQL convert builtin?

If so, running the following probably works:

select ... order by convert(invnumber as unsigned) asc;

In MySQL, at least, that properly sorts:

2
1
10
9C

into:

1
2
9C
10

Alternatively, Would it be possible to group by length? I'm not sure how to pull it off, but the normal string sort would work, if two character strings were grouped after the fact with other two character strings, etc..

Sorry, but I have done far more in MySQL environments than in PSQL. I'm too familiar with the non-ANSI extensions.

Luke