Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Postgres. How to convert string that contains '\' to bytea?

I have string 'test\data' or just one backslash symbol '\'.

How it convert to bytea?

like image 308
marvinorez Avatar asked Dec 21 '13 10:12

marvinorez


People also ask

What is Bytea in Postgres?

The bytea data type allows the storage of binary strings or what is typically thought of as “raw bytes”. Materialize supports both the typical formats for input and output: the hex format and the historical PostgreSQL escape format. The hex format is preferred.

How do I remove a character from a string in PostgreSQL?

The LTRIM() function removes all characters, spaces by default, from the beginning of a string. The RTRIM() function removes all characters, spaces by default, from the end of a string. The BTRIM function is the combination of the LTRIM() and RTRIM() functions.

How do I select a string in PostgreSQL?

SUBSTRING() function The PostgreSQL substring function is used to extract a string containing a specific number of characters from a particular position of a given string. The main string from where the character to be extracted. Optional. The position of the string from where the extracting will be starting.

What is decode in PostgreSQL?

PostgreSQL DECODE() function is used to decode or extract the binary data from the input string, which is in textual format and which has been encoded by using PostgreSQL Encode() function.


1 Answers

Backlashes need special handling if casted from to bytea see src/backend/utils/adt/varlena.c.

Thus escape each backslash using replace('test\data', '\', '\\')::bytea before casting to bytea.

You could also use the already suggested function convert_to(text, encoding) bytea. But note that this function is not IMMUTABLE and thus it can't be used in any context out of the box.

like image 106
FireEmerald Avatar answered Oct 10 '22 07:10

FireEmerald