AndrewMontana AndrewMontana -4 years ago 100
SQL Question

Can't get element from array in second column PostgreSQL

I made two dimensional array as timestamp type. Then i added an insert like this

'{{2017-03-01 14:00:00},{2017-06-01 21:00:00}}'

How can I get only second element?

Creating of table:

create table client(id SERIAL PRIMARY KEY, full_name VARCHAR(40) NOT NULL, gender VARCHAR(15) NOT NULL, phone_number VARCHAR(20) NOT NULL, card_type VARCHAR(30) NOT NULL, card_period Timestamp[][] NOT NULL );

Then I made some inserts as next:

insert into client (full_name, phone_number, card_type, card_period, gender) values('Mr James Abrams','3805832940003073','Bronze','{{2017-03-01 14:00:00},{2017-06-01 21:00:00}}','man');

When for example I select something

select card_period from client where id = 1;

Its shows like: 2017-03-01T14:00:00,2017-06-01T21:00:00
I tried to select card_period[0:1] - shows only first, if [0:2] then it shows them both

Answer Source

The answer already given before me is correct in format, just number of array order are wrong :)

This is how you get 2nd element value:

SELECT card_period[2][1] FROM client WHERE id = 1;
 2017-06-01 21:00:00
(1 row)
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download