Re: How to remove n-first elements from array? - Mailing list pgsql-general

From Merlin Moncure
Subject Re: How to remove n-first elements from array?
Date
Msg-id CAHyXU0wBJsuWVJ0RU0nq93kxYQ+C7mcBTo5Z9DqKHD6EAG757A@mail.gmail.com
Whole thread Raw
In response to Re: How to remove n-first elements from array?  (aasat <satriani@veranet.pl>)
Responses Re: How to remove n-first elements from array?
List pgsql-general
On Mon, Dec 17, 2012 at 3:27 AM, aasat <satriani@veranet.pl> wrote:
> I found solution
>
> do $$
> declare
>   v_array integer[] := array[1,2,3,4,5];
>   v_array2 integer[];
>   v_delete_elements integer :=2;
>   v_new_index integer;
> begin
>   v_new_index := array_lower(v_array, 1) + v_delete_elements;
>
>   v_array2 := array_fill(v_array[v_new_index], ARRAY[1], ARRAY[v_new_index])
> ||   v_array[v_new_index+1:array_upper(v_array, 1)];
>   raise notice '%', v_array2;
> end;
> $$
>
> NOTICE:  [3:5]={3,4,5}

here's a solution that's a bit more general:
create or replace function remove_front(a anyarray, nkeep int) returns
anyarray as
$$
  select a[array_upper(a, 1) - (nkeep - 1):array_upper(a, 1)];
$$ language sql immutable;

merlin


pgsql-general by date:

Previous
From: "Kevin Grittner"
Date:
Subject: Re: problem with large inserts
Next
From: hubert depesz lubaczewski
Date:
Subject: Re: [COMMITTERS] pgsql: Allow a streaming replication standby to follow a timeline switc