Search code examples
arrayspostgresqlpostgresql-17

Remove first and last element from an array


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.


Solution

  • trim_array() is there since PostgreSQL 14:

    select (trim_array(array[1,2,3],1))[2:];