我有一个索引列,字符串类型,其中的数据格式如" 1.5.4.10.7"。
我需要进行查询,返回排序的值,好像该列是通过在'上分割字符串而得到的整数数组。字符。
我可以用:
提取数组select string_to_array(index,'.') as index_array from performance_indicators;
我怎样才能1)将数组元素转换为整数,2)对整数数组进行排序?
答案 0 :(得分:1)
您可以简单地将字符串数组转换为整数数组,并按值排序,例如:
with a_table(col) as (
values ('1.2.3.4'), ('10.2.3.4'), ('3.4.5.6')
)
select string_to_array(col, '.')::int[]
from a_table
order by 1;
string_to_array
-----------------
{1,2,3,4}
{3,4,5,6}
{10,2,3,4}
(3 rows)