Philip Warner <pjw@rhyme.com.au> writes:
> At least in 7.3.2 we can't cast int to varbit. Worse still (and maybe I am
> missing something):
> # select cast(8 as bit(10));
> bit
> ------------
> 0000000000
Not sure we have a lot of choice there. The basic cast is to bit(32).
We have
regression=# select cast(8 as bit(32)); bit
----------------------------------00000000000000000000000000001000
(1 row)
regression=# select cast(8 as bit(32)) :: bit(10); bit
------------0000000000
(1 row)
If you make the direct cast do something different, it'll be
inconsistent.
What is perhaps needed is a bitstring length adjustment operation that
allows padding or truncation to occur at the left instead of the right.
The semantics of cast are already spoken for, but we could provide this
as a function.
As for int->varbit, you can get there easily enough:
regression=# select cast(8 as bit(32)) :: varbit; varbit
----------------------------------00000000000000000000000000001000
(1 row)
regards, tom lane