Home > Mobile >  How to access individual elements of user defined data types?
How to access individual elements of user defined data types?

Time:01-15

This is a Postgres SQL question.

I am having a user defined data type as follows.

create type my_type as (name varchar(100), credit_points double precision)

I am having a table which uses the above type as column's datatype as below.

create table tab1 (
    id serial,
    name_credit my_type
)

I could insert values into this table using below insert statement.

insert into tab1 (name_credit) values(('santhosh', 101.75)::my_type)

Now, I want to access the table like below.

select * from tab1 where name_credit[2] between 100.0 and 110.0

or

select * from tab1 where name_credit.credit_points between 100.0 and 110.0

But neither of it is working for me. Any ideas please?

CodePudding user response:

Put the column name in ().

SELECT *
       FROM tab1
       WHERE (name_credit).credit_points BETWEEN 100.0
                                                 AND 110.0;

See "8.16.3. Accessing Composite Types" for more details.

  •  Tags:  
  • Related