Re: Ordenar resultado por números en campo varchar

From: Oswaldo Hernández <listas(at)soft-com(dot)es>
To: pgsql-es-ayuda(at)postgresql(dot)org
Subject: Re: Ordenar resultado por números en campo varchar
Date: 2010-10-01 15:57:20
Message-ID: 4CA604E0.60103@soft-com.es
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-es-ayuda

El 30/09/2010 18:22, Rodrigo Ruiz escribió:
> Estimados,
> la siguiente representa parte del resultado de una consulta sobre una
> tabla y que se ordena por el campo 'indice' de forma ascendente.
>
> # select * from tabla order by indice asc;
>
> id | titulo | indice
> ----+----------+--------
> 106 | texto 4 | 1.1
> 115 | texto 13 | 1.10
> 116 | texto 1 | 1.11
> 107 | texto 5 | 1.2
> 108 | texto 6 | 1.3
> 109 | texto 7 | 1.4
> 110 | texto 8 | 1.5
> 111 | texto 9 | 1.6
> 112 | texto 10 | 1.7
> 113 | texto 11 | 1.8
> 114 | texto 12 | 1.9
> 117 | texto 2 | 2.1
> 118 | texto 3 | 2.2
>
> El campo indice es un varchar, si notan, luego del punto 1.1 salta al
> 1.10, el cual necesito se presente siguiendo al punto 1.9.
> Intenté pasando el campo a numérico con cast, sin embargo se me
> complicó, pues podrían existir puntos del tipo 1.2.6 o más detallado aún.
> La pregunta ya la podrán adivinar, cómo podría obtener el resultado de
> la consulta de manera ordenada por índice?
>

select indice
from (values ('4.0.3.2'), ('1.2.3.4'), ('2.3.4.5'),
('20.3.1'), ('20.3'), ('4.1'), ('10.3'),
('0.5'), ('10.2.7')
) foo(indice)
order by regexp_split_to_array(indice, E'\\.')::integer[];

indice
---------
0.5
1.2.3.4
2.3.4.5
4.0.3.2
4.1
10.2.7
10.3
20.3
20.3.1
(9 filas)

:)

--
Oswaldo Hernández

In response to

Responses

Browse pgsql-es-ayuda by date

  From Date Subject
Next Message Marcelo Retamal 2010-10-01 16:56:18 Problemas con PgAdmin y PG9.0
Previous Message Desarrollo Escuintla 2010-10-01 15:42:55 Re: Concesionaria de Vehiculos