Shuumi
Shuumi

Reputation: 76

Get an array out of a two dimensional array in PostgreSQL

In PostgreSQL, I have a two dimensional array like:

SELECT ARRAY[[1,2,3],[4,5,6]];

and out of this array, I want to retrieve the whole 1st ([1,2,3]) array. Sadly something like:

SELECT (ARRAY[[1,2,3],[4,5,6]])[1];

doesn't work, since it returns a null.

Is it possible?

Upvotes: 1

Views: 2548

Answers (2)

McNets
McNets

Reputation: 10807

create table test(id int, val int[][]);
insert into test values (1, ARRAY[[1,2,3],[4,5,6]]);
✓

1 rows affected
select id, val[1:1] from test;
id | val      
-: | :--------
 1 | {{1,2,3}}
SELECT (ARRAY[[1,2,3],[4,5,6]])[1:1]
| array     |
| :-------- |
| {{1,2,3}} |

dbfiddle here

Upvotes: 1

Shuumi
Shuumi

Reputation: 76

Okay, i worked it out:

SELECT (ARRAY[[1,2,3],[4,5,6]])[1][1:3]

does exacly what I wanted.

Upvotes: 0

Related Questions