Thread: Type conversion

Type conversion

From
Ice Planet
Date:
Hello

I have following situation

create table t1 (a int8 primary key, b int8);
create table t2 (x int2);
insert into t1 values (1,1);

A: insert into t2 values (select b from t1 where a = 1)

B: insert into t2 values (select int2(int4(b)) from t1 where a = 1)

Both possibilites fail, how can i do this insert???

Thanks for help
                      Best regards                                             Ice Planet

e-mail: ice@adiemus.sk
ICQ#: 67765483




Re: Type conversion

From
Tom Lane
Date:
Ice Planet <ice@adiemus.sk> writes:
> B: insert into t2 values (select int2(int4(b)) from t1 where a = 1)

Works for me when spelled correctly:

regression=# insert into t2 (select int2(int4(b)) from t1 where a = 1);
INSERT 314647 1

If you make a suitable conversion function then you can omit the
explicit conversion:

regression=# create function int2(int8) returns int2 as
regression-# 'begin return int2(int4($1)); end;' language 'plpgsql';
CREATE
regression=# insert into t2 (select b from t1 where a = 1);
INSERT 314649 1
        regards, tom lane


Re: Type conversion

From
Ed Loehr
Date:
Tom Lane wrote:
> 
> Ice Planet <ice@adiemus.sk> writes:
> > B: insert into t2 values (select int2(int4(b)) from t1 where a = 1)
> 
> Works for me when spelled correctly:

I think you can also leave out the 'values' for a sub-select insert,
though I haven't checked to see if it matters...

Regards,
Ed Loehr