pg.ddx.io  pgsql-sql@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Scott Royston <scroyston@mac.com>
To: pgsql-sql@postgresql.org
Cc: pgsql-hackers@postgresql.org
Subject: Re: [SQL] can't cast varchar as integer?
Date: Tue, 14 May 2002 18:02:41 -0500
Message-ID: <B1AEDD10-678E-11D6-B480-000393577144@mac.com> (raw)
In-Reply-To: <1021412859.992.2468.camel@ciderbox>

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
>




view thread (5+ messages)  latest in thread

Message-ID: <B1AEDD10-678E-11D6-B480-000393577144@mac.com>
Permalink:  ../B1AEDD10-678E-11D6-B480-000393577144@mac.com/
Also on:    postgresql.org/message-id/B1AEDD10-678E-11D6-B480-000393577144@mac.com

 · 

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 using the --to and --cc options:
  reply via email

  To: pgsql-sql@postgresql.org
  Cc: scroyston@mac.com, pgsql-hackers@postgresql.org
  Subject: Re: [SQL] can't cast varchar as integer?
  In-Reply-To: <B1AEDD10-678E-11D6-B480-000393577144@mac.com>

* 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