PostgreSQL - Extract value out of an array with position

Viewed 243

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?

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];
Related