2

I have a column in a Postgres database that is of type varchar but has a binary value stored in it. How can I return the binary value of the column in a way I can read it?

For example, at the moment I see "r" in the column, I want to see the 1's and 0's that make up the value for r.


To be a bit more clear about what I want. I think the application is storing data about ticked checkboxes in binary. So for a group of checkboxes:

  • Unchecked (0)
  • Checked (1)
  • Checked (1)
  • Checked (1)
  • Unchecked (0)
  • Unchecked (0)
  • Checked (1)
  • Unchecked (0)

it stores the value "r" and I want to see the binary or hex of the value that is stored. So for the value "r" I want to get the hex value "72" or the binary value "0111 0010"

2 Answers 2

4

Storing binary data in a text column is not a good idea. You can use the type bytea, e.g.:

drop table if exists my_table;
create table my_table(id serial primary key, switch bytea);
insert into my_table (switch) values
('\x7272'),
('\x1111'),
('\xffff');

You can easily set and get values in hex format, convert them to bit strings, get/set individual bytes/bits, e.g.:

select id, 
    switch,
    right(switch::text, -1)::bit(16) as bits, 
    right(switch::text, -1)::bit(16)::int as int,
    get_byte(switch, 0)
from my_table;

 id | switch |       bits       |  int  | get_byte 
----+--------+------------------+-------+----------
  1 | \x7272 | 0111001001110010 | 29298 |      114
  2 | \x1111 | 0001000100010001 |  4369 |       17
  3 | \xffff | 1111111111111111 | 65535 |      255
(3 rows)    

You can cast a text (varchar) to bytea:

select 'r'::bytea;

 bytea 
-------
 \x72
(1 row) 

Note that in some tools (e.g. PgAdmin III) you should set the parameter to get hex output:

set bytea_output to hex;

Per the documentation:

The output format depends on the configuration parameter bytea_output; the default is hex. (Note that the hex format was introduced in PostgreSQL 9.0; earlier versions and some tools don't understand it.)

Read also in the documentation:

Sign up to request clarification or add additional context in comments.

Comments

0

Using varchar for this is a bad idea. For example, you cannot store zero bytes that way.

Anyway, the answer to your question should be a simple type cast:

CAST(textcol AS bytea)

5 Comments

this changed the data type but hasn't changed how it is being displayed for me.
Then try casting the bytea to bit varying.
This does not work if textcol contains backslashes.
@cstork Does doubling the backslashes help?
Yes, exactly. ;-) I just had this problem and maybe you know a way to solve this without this ugly replace(textcol, '\', '\\') detour? It seems to me that somehow it has to be possible to access the binary representation of textcol without first transforming the whole string just to receive what you started with but with type bytea. Any idea?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.