Re: type cast about int to bit

From: Adrian Klaver <adrian(dot)klaver(at)gmail(dot)com>
To: pgsql-sql(at)postgresql(dot)org
Cc: zoulx1982 <zoulx1982(at)163(dot)com>
Subject: Re: type cast about int to bit
Date: 2012-02-07 01:19:11
Message-ID: 201202061719.12213.adrian.klaver@gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-sql

On Sunday, February 05, 2012 10:11:12 pm zoulx1982 wrote:
> hi,
> there is a problem about type cast that i don't understand, follow is my
> test.
>
> postgres=# select 10::bit(3);
> bit
> -----
> 010
> (1 row)
> postgres=# select 10::bit varying(3);
> ERROR: cannot cast type integer to bit varying
> LINE 1: select 10::bit varying(3);
> ^
> postgres=#
>
> my question is why int can cast to bit , i want to know the reason.
> thank you for your timing.

My guess it depends on the fact that bit types are stored as either char or
varchar depending on whether they are bit or bit varying.
In the first case you are basically doing an int-->char, for which there is a
built in cast.
In the second case you are doing int-->varchar for which there is not a cast.

--
Adrian Klaver
adrian(dot)klaver(at)gmail(dot)com

In response to

Browse pgsql-sql by date

  From Date Subject
Next Message Adrian Klaver 2012-02-07 03:28:55 Re: type cast about int to bit
Previous Message Jasen Betts 2012-02-06 11:05:15 Re: type cast about int to bit