pg.ddx.io  pgsql-sql@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: George Moga <george@flex.ro>
To: Larry Bottorff <swprenzl@southwind.net>
To: SQL PostgreSQL <pgsql-sql@postgreSQL.org>
Subject: Re: [SQL] int4 to varchar conversion
Date: Thu, 07 Jan 1999 18:07:16 +0200
Message-ID: <3694DBB4.CCF4DB9D@flex.ro> (raw)
References: <Pine.BSI.4.02A.9901070928360.239-100000@jasper.southwind.net>

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

view thread (3+ messages)  latest in thread

Message-ID: <3694DBB4.CCF4DB9D@flex.ro>
Permalink:  ../3694DBB4.CCF4DB9D@flex.ro/
Also on:    postgresql.org/message-id/3694DBB4.CCF4DB9D@flex.ro

 · 

reply

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Reply to all the recipients from your mail client; the link fills in
  the headers below: reply via email
  Some clients, webmail especially, drop In-Reply-To and References from
  a link, and the reply then starts a new thread: use the mbox below.

  To: pgsql-sql@postgresql.org
  Cc: george@flex.ro, swprenzl@southwind.net
  Subject: Re: [SQL] int4 to varchar conversion
  In-Reply-To: <3694DBB4.CCF4DB9D@flex.ro>
  References: <Pine.BSI.4.02A.9901070928360.239-100000@jasper.southwind.net> <3694DBB4.CCF4DB9D@flex.ro>

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox