w3resource

PostgreSQL ARRAY_PREPEND() function

ARRAY_PREPEND() function

The ARRAY_PREPEND() function in PostgreSQL adds an element to the beginning of an array. This function is useful for expanding arrays by inserting new elements at the start, thereby shifting existing elements to higher indices.

Uses of the PostgreSQL ARRAY_PREPEND() Function
  • Add Elements: Insert a new element at the start of an array.

  • Shift Array Contents: Move existing elements to higher indices by prepending new values.

  • Initialize Arrays: Start arrays with specific leading elements.

  • Dynamic Array Construction: Build arrays dynamically by adding elements to the beginning.

Syntax:

array_prepend (anyelement, anyarray)

Return Type:

anyarray

PostgreSQL Version: 9.3

Example: PostgreSQL ARRAY_PREPEND() function

Code:

SELECT array_prepend(1, ARRAY[2,3]);

Sample Output:

 array_prepend
---------------
 {1,2,3}
(1 row)

Previous: ARRAY_LOWER function
Next: ARRAY_REMOVE function



Become a Patron!

Follow us on Facebook and Twitter for latest update.

It will be nice if you may share this link in any developer community or anywhere else, from where other developers may find this content. Thanks.

https://www.w3resource.com/PostgreSQL/postgresql_array_prepend-function.php