Re: Sorting by numerical order

From: George Essig <george_essig(at)yahoo(dot)com>
To: pgsql-general(at)postgresql(dot)org
Subject: Re: Sorting by numerical order
Date: 2004-03-15 20:12:12
Message-ID: 20040315201212.93490.qmail@web80206.mail.yahoo.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

Steve Atkins wrote:
>
> Couldn't you just use a regex match? foo ~ '-?[0-9]+(\.[0-9]+)?' or
> somesuch.
>
> Cheers,
> Steve

This helps a lot. I used a similar regular expression, the substring function, and the order by
clause to do the following:

test=# select * from sort_test order by name;
name
--------
10
aaa
abc
abc 1
abc 10
abc 2
abc 20
(7 rows)

test=# select name, substring(name from '^(.*?)([-+]?[0-9]+(\\.[0-9]+)?)?$') as start_string,
substring(name from '[-+]?[0-9]+(\\.[0-9]+)?$')::float as end_number from sort_test order by
start_string, end_number;
name | start_string | end_number
--------+--------------+------------
10 | | 10
aaa | aaa |
abc | abc |
abc 1 | abc | 1
abc 2 | abc | 2
abc 10 | abc | 10
abc 20 | abc | 20
(7 rows)

George Essig

Browse pgsql-general by date

  From Date Subject
Next Message Shilong Stanley Yao 2004-03-15 20:14:40 Re: Character escape in "CREATE FUNCTION ..."
Previous Message Edwin Pauli 2004-03-15 20:11:20 Re: PostgeSQL problem (server crashed?)