skip to Main Content

I can remove easily the first element from an array:

SELECT (ARRAY[1,2,3])[2:]
--{2,3}

What is the simplest way to remove the last element as well ?
The size is dynamic and is not known at compile time.

2

Answers


  1. You can use the array length function to get the array length:

    SELECT (ARRAY[1,2,3])[2:array_length(ARRAY[1,2,3], 1) - 1];
                          ^ Beginning                       ^ Length - 1 = End
    
    
    Login or Signup to reply.
  2. trim_array() is there since PostgreSQL 14:

    select (trim_array(array[1,2,3],1))[2:];
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search