Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PostgreSQL - Extract value out of an array with position

Tags:

postgresql

I want to extract a value out of an array of numerical values with index position in PostgreSQL.

My array is like {0.10,0.20,0.30}, type is numeric[], array position is an integer calculated with array_position. Syntax will be SELECT myarray[array_position];, but when I try (for example) :

SELECT'{0.10,0.20,0.30}'::numeric[][1];

It returned me the entire array.

How to extract a value out of an array with index position in PostgreSQL?

like image 642
GeoGyro Avatar asked Sep 18 '25 12:09

GeoGyro


1 Answers

In your example [1] is the part of array definition: https://www.postgresql.org/docs/current/arrays.html#ARRAYS-DECLARATION

However, the current implementation ignores any supplied array size limits, i.e., the behavior is the same as for arrays of unspecified length.

Use parentheses to split the definition and reference:

SELECT ('{0.10,0.20,0.30}'::numeric[])[1];
like image 160
Abelisto Avatar answered Sep 21 '25 06:09

Abelisto