Thread: int4 to varchar conversion

int4 to varchar conversion

From
Larry Bottorff
Date:
I'm doing something very typical: I created a sequence "seq" and then
wanted to insert the NEXTVAL('seq') into a varchar field of a table to be
used as a primary key.

INSERT INTO products (prodno, ...) VALUES (NEXTVAL('seq'), 'blah',
'blah');

Where prodno is varchar. I tried a text(...) cast, but it complains of a
text-varchar mismatch. There is no varchar(...).

Apparently, this is not possible in Postgres(?).


Larry Bottorff                SouthWind Internet Access, Inc.
System Programmer            120 S. Market St. Suite 300
swprenzl@southwind.net            Wichita, KS 67202
---------------------------------------------------------------------
In Wichita:(316)263-7964        Elsewhere:1-877-525-7964


Re: [SQL] int4 to varchar conversion

From
George Moga
Date:
Larry Bottorff wrote:
I'm doing something very typical: I created a sequence "seq" and then
wanted to insert the NEXTVAL('seq') into a varchar field of a table to be
used as a primary key.

INSERT INTO products (prodno, ...) VALUES (NEXTVAL('seq'), 'blah',
'blah');

Where prodno is varchar. I tried a text(...) cast, but it complains of a
text-varchar mismatch. There is no varchar(...).

Apparently, this is not possible in Postgres(?).

Larry Bottorff                          SouthWind Internet Access, Inc.
System Programmer                       120 S. Market St. Suite 300
swprenzl@southwind.net                  Wichita, KS 67202
---------------------------------------------------------------------
In Wichita:(316)263-7964                Elsewhere:1-877-525-7964


try:

INSERT INTO products (prodno, ...) VALUES (NEXTVAL('seq')::text, 'blah', ...);
 

Example:

test=>CREATE TABLE seq_varchar (field1 varchar(4), field2 text, primary key (field1));
NOTICE:  CREATE TABLE/PRIMARY KEY will create implicit index var_pkey for table var
CREATE
test=>CREATE SEQUENCE seq_test;
test=>INSERT INTO seq_varchar VALUES (NEXTVAL ( 'seq_test' )::text, 'blablabla');
INSERT 741504 1
test=>SELECT * FROM seq_varchar;
field1|field2
------+---------
     1|blablabla
(1 row)

test=>INSERT INTO seq_varchar VALUES (NEXTVAL ( 'seq_test' )::text, 'blahblahblah');
INSERT 741505 1
test=>SELECT * FROM seq_varchar;
field1|field2
------+------------
     1|blablabla
     2|blahblahblah
(2 rows)

-- 
Best,
        George Moga,
        george@flex.ro
        george@cicnet.ro
        Braila, ROMANIA

Re: [SQL] int4 to varchar conversion

From
Larry Bottorff
Date:
Tried your suggestion, but. . . .

=> create table seq_varchar (field1 varchar(4), field2 text, CONSTRAINT
pk_sq PRIMARY KEY (field1));

=> insert into seq_varchar values (NEXTVAL ( 'seq_test' )::text,
'blablabla');
ERROR:  parser: attribute 'field1' is of type 'varchar' but expression is
of type 'text'

Is this a version or settings issue? We're not 6.4, I know that much. . .

Larry Bottorff                    SouthWind Internet Access, Inc.
System Programmer                 120 S. Market St. Suite 300
swprenzl@southwind.net            Wichita, KS 67202
---------------------------------------------------------------------
In Wichita:(316)263-7964              Elsewhere:1-877-525-7964