sort by numeric part of a field

Hello,
I must sort a table width only one column. Inside the field I have, for
example, hard disk capacity (something like '100Gb' or '80Gb' or
'80Gbyte' or 'Gb80').

So, if I use ORDER BY ... DESC, 80Gb will be before 100Gb and other
errors.

How can i sort the table only on the numeric part of the field?

I'm trying using CAST() but with no results.

Thank you very much
Andrea
andrea.demagistris [ Mi, 26 Juli 2006 12:35 ] [ ID #1405606 ]

Re: sort by numeric part of a field

I'd say to try to substring for G (both lower and upper case and any other
letters you may need for example M) and then drop everything after that
position. This will leave you only the digits.

-s


On 7/26/06 06:35, in article
1153910126.921460.51390 [at] m73g2000cwd.googlegroups.com,
"andrea.demagistris [at] libero.it" <andrea.demagistris [at] libero.it> wrote:

> Hello,
> I must sort a table width only one column. Inside the field I have, for
> example, hard disk capacity (something like '100Gb' or '80Gb' or
> '80Gbyte' or 'Gb80').
>
> So, if I use ORDER BY ... DESC, 80Gb will be before 100Gb and other
> errors.
>
> How can i sort the table only on the numeric part of the field?
>
> I'm trying using CAST() but with no results.
>
> Thank you very much
> Andrea
>
Shawn Hamzee [ Mi, 26 Juli 2006 16:15 ] [ ID #1405608 ]

Re: sort by numeric part of a field

andrea.demagistris [at] libero.it wrote:
> How can i sort the table only on the numeric part of the field?

....ORDER BY 0 + harddiskcapacity

This coerces the column to be evaluated as an integer. It uses the
digits in the leading portion of the value, to form an integer.
In other words, the expression 0 + '80Gb' returns the integer value 80.

Regards,
Bill K.
Bill Karwin [ So, 30 Juli 2006 03:00 ] [ ID #1410415 ]

Re: sort by numeric part of a field

yes!
very good, you solved my problem!
Thank you very much
Andrea

Bill Karwin ha scritto:

> andrea.demagistris [at] libero.it wrote:
> > How can i sort the table only on the numeric part of the field?
>
> ...ORDER BY 0 + harddiskcapacity
>
> This coerces the column to be evaluated as an integer. It uses the
> digits in the leading portion of the value, to form an integer.
> In other words, the expression 0 + '80Gb' returns the integer value 80.
>
> Regards,
> Bill K.
andrea.demagistris [ Di, 01 August 2006 15:51 ] [ ID #1412927 ]
Datenbanken » mailing.database.mysql » sort by numeric part of a field

Vorheriges Thema: MySQL 4 vs 5 - threading model change
Nächstes Thema: Linking Tables