Re: Sorting by numerical order - Mailing list pgsql-general

From George Essig
Subject Re: Sorting by numerical order
Date
Msg-id 20040315201212.93490.qmail@web80206.mail.yahoo.com
Whole thread Raw
In response to Sorting by numerical order  (CSN <cool_screen_name90001@yahoo.com>)
List 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

pgsql-general by date:

Previous
From: Edwin Pauli
Date:
Subject: Re: PostgeSQL problem (server crashed?)
Next
From: Shilong Stanley Yao
Date:
Subject: Re: Character escape in "CREATE FUNCTION ..."