Received: from localhost.localdomain (postgresql.org [64.49.215.8]) by localhost (Postfix) with ESMTP id 7ADE0475961; Tue, 14 May 2002 19:03:01 -0400 (EDT) Received: from smtpout.mac.com (smtpout.mac.com [204.179.120.86]) by postgresql.org (Postfix) with ESMTP id A2DF7475935; Tue, 14 May 2002 19:02:46 -0400 (EDT) Received: from smtp-relay04-en1.mac.com (smtp-relay04-en1 [10.13.10.223]) by smtpout.mac.com (8.12.1/8.10.2/1.0) with ESMTP id g4EN2nEM005018; Tue, 14 May 2002 16:02:49 -0700 (PDT) Received: from asmtp02.mac.com (asmtp02-qfe3.mac.com [10.13.10.66]) by smtp-relay04-en1.mac.com (8.12.1/8.12.1/1.0) with ESMTP id g4EN2h2J019432; Tue, 14 May 2002 16:02:43 -0700 (PDT) Received: from localhost ([138.88.66.115]) by asmtp02.mac.com (Netscape Messaging Server 4.15 asmtp02 Jun 21 2001 23:53:48) with ESMTP id GW4IOJ00.2BV; Tue, 14 May 2002 16:02:43 -0700 Date: Tue, 14 May 2002 18:02:41 -0500 Subject: Re: [SQL] can't cast varchar as integer? Content-Type: text/plain; charset=US-ASCII; format=flowed Mime-Version: 1.0 (Apple Message framework v481) Cc: pgsql-hackers@postgresql.org To: pgsql-sql@postgresql.org From: Scott Royston In-Reply-To: <1021412859.992.2468.camel@ciderbox> Message-Id: Content-Transfer-Encoding: 7bit X-Mailer: Apple Mail (2.481) X-Archive-Number: 200205/664 X-Sequence-Number: 22869 Thanks for the replies so far. I had been using cast(foo::text as integer). To clarify my question, does anyone know *why* I can't cast from varchar to integer? Why should I have to cast to text first? thanks On Tuesday, May 14, 2002, at 04:47 PM, David Stanaway wrote: > On Tue, 2002-05-14 at 13:56, Scott Royston wrote: >> Mac OSX, postgresql 7.2.1 >> >> what's the reasoning behind not being able to cast a varchar as >> integer? this seems very weird to me: >> >> LEDEV=# create table test (foo varchar(5), bar text); >> LEDEV=# insert into test (foo, bar) values ('123', '123'); >> LEDEV=# select cast(foo as integer) from test; >> ERROR: Cannot cast type 'character varying' to 'integer' >> LEDEV=# select cast(bar as integer) from test; >> bar >> ----- >> 123 >> (1 row) > > > Try this: > > scratch=# select foo::text::integer from test; > foo > ----- > 123 > (1 row) > > > Or: > > scratch=# select int4(foo) from test; > int4 > ------ > 123 > (1 row) > > > > -- > David Stanaway >