| From: | Scott Royston <scroyston(at)mac(dot)com> | 
|---|---|
| To: | pgsql-sql(at)postgresql(dot)org | 
| Cc: | pgsql-hackers(at)postgresql(dot)org | 
| Subject: | Re: [SQL] can't cast varchar as integer? | 
| Date: | 2002-05-14 23:02:41 | 
| Message-ID: | B1AEDD10-678E-11D6-B480-000393577144@mac.com | 
| Views: | Whole Thread | Raw Message | Download mbox | Resend email | 
| Thread: | |
| Lists: | pgsql-hackers pgsql-sql | 
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
>
| From | Date | Subject | |
|---|---|---|---|
| Next Message | Tom Lane | 2002-05-14 23:15:14 | Re: SRF patch (was Re: [HACKERS] troubleshooting pointers) | 
| Previous Message | Joe Conway | 2002-05-14 22:27:47 | Re: SRF patch (was Re: [HACKERS] troubleshooting pointers) | 
| From | Date | Subject | |
|---|---|---|---|
| Next Message | Tom Lane | 2002-05-14 23:29:10 | Re: can't cast varchar as integer? | 
| Previous Message | Oleg Lebedev | 2002-05-14 22:15:00 | Re: pg_dumpall |