4

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?

0

1 Answer 1

7

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];
Sign up to request clarification or add additional context in comments.

1 Comment

I don't know I have to split definition and reference with parentheses but it's logic when you write it.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.