pg.ddx.io  pgsql-sql@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Joel Burton <joel@joelburton.com>
To: Frank Morton <fmorton@base2inc.com>
To: pgsql-sql@postgresql.org
Subject: Re: sort text field numerically
Date: Tue, 14 May 2002 08:43:19 -0400
Message-ID: <JGEPJNMCKODMDHGOBKDNOEBHCOAA.joel@joelburton.com> (raw)
In-Reply-To: <020101c1fb42$3665f770$8455e5ce@edison>

> -----Original Message-----
> From: pgsql-sql-owner@postgresql.org
> [mailto:pgsql-sql-owner@postgresql.org]On Behalf Of Frank Morton
> Sent: Tuesday, May 14, 2002 8:24 AM
> To: pgsql-sql@postgresql.org
> Subject: [SQL] sort text field numerically
>
>
> I have a table with a column called "weight" that
> is a text field, but in this case, contains float values,
> such as ".8" or "0.25". Might also be null.
>
> How can I sort them in numeric order instead of
> textual order? Thanks.


  select * from floats_table order by text_float_field::float;

will work, as long as there are no non-float items in the table (if there
are, you could use an ORDER BY CASE ... to look for non-numeric characters
and sort these differently, and sort the rest by converting to floats as
::float)

HTH.

Joel BURTON | joel@joelburton.com | joelburton.com | aim: wjoelburton
Knowledge Management & Technology Consultant




view thread (2+ messages)

Message-ID: <JGEPJNMCKODMDHGOBKDNOEBHCOAA.joel@joelburton.com>
Permalink:  ../JGEPJNMCKODMDHGOBKDNOEBHCOAA.joel@joelburton.com/
Also on:    postgresql.org/message-id/JGEPJNMCKODMDHGOBKDNOEBHCOAA.joel@joelburton.com

 · 

reply

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Reply to all the recipients using the --to and --cc options:
  reply via email

  To: pgsql-sql@postgresql.org
  Cc: joel@joelburton.com, fmorton@base2inc.com
  Subject: Re: sort text field numerically
  In-Reply-To: <JGEPJNMCKODMDHGOBKDNOEBHCOAA.joel@joelburton.com>

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox