I need to decode a base64 string and take a chunk of binary.
Is there a SQL function in Postgres to simply convert a bytea
into a binary string representation?
(Like "00010001010101010".)
If your Postgres installation runs with the default setting bytea_output = 'hex'
, there is a very simple hack:
SELECT right(bytea_col::text, -1)::varbit;
Example:
SELECT right((bytea '\xDEADBEEF')::text, -1)::varbit;
Result:
'11011110101011011011111011101111'
right(text, -1)
is just the cheapest way to remove the leading backslash from the text representation.
varbit
(standard SQL name bit varying
) is for bit strings of arbitrary length. Cast the result to text
or varchar
if you like.
Related, with explanation: