PostgreSQL: manipulate binary data - change last byte with SQL command

859 Views Asked by At

In a PostgreSQL table I have a column file_bytes which has the data type bytea.

I am looking for a simple SQL statement to manipulate only the last byte of the content of this column.

1

There are 1 best solutions below

0
On BEST ANSWER

demo: db<>fiddle

UPDATE test 
SET file_bytes = overlay(file_bytes placing 'X'::bytea from octet_length(file_bytes));

https://www.postgresql.org/docs/current/static/functions-binarystring.html

octet_length() gives the number of bytes of binary data. overlay() allows to rewrite data from a certain position.