Pergunta

I have a long string contained of int,int,float,float * many and I need to get the last two floats.

100,140,14.123,15.123,200,240,16.124,17.123

I'm using string_to_array to get element 3 & 4 (first two floats) but I also need a way to get the last two floats (places 7 & 8 in this example).

Foi útil?

Solução

try

CREATE OR REPLACE FUNCTION last_fields(anyarray, int)
RETURNS anyarray LANGUAGE sql AS $$
  SELECT ($1)[array_upper($1,1) - $2 + 1: array_upper($1,1)];
$$;

postgres=# select last_fields(ARRAY[1,2,3,4,5], 2);
 last_fields 
─────────────
 {4,5}
(1 row)
Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top