Supported Versions: Current (17) / 16 / 15 / 14 / 13
Development Versions: devel
Unsupported versions: 12 / 11 / 10 / 9.6 / 9.5 / 9.4 / 9.3 / 9.2 / 9.1 / 9.0 / 8.4 / 8.3 / 8.2 / 8.1 / 8.0 / 7.4 / 7.3 / 7.2
This documentation is for an unsupported version of PostgreSQL.
You may want to view the same page for the current version, or one of the other supported versions listed above instead.

9.5. Binary String Functions and Operators

This section describes functions and operators for examining and manipulating values of type bytea.

SQL defines some string functions with a special syntax where certain key words rather than commas are used to separate the arguments. Details are in Table 9-8. Some functions are also implemented using the regular syntax for function invocation. (See Table 9-9.)

Table 9-8. SQL Binary String Functions and Operators

FunctionReturn TypeDescriptionExampleResult
string||stringbyteaString concatenation E'\\\\Post'::bytea || E'\\047gres\\000'::bytea\\Post'gres\000
get_bit(string, offset)intExtract bit from string get_bit(E'Th\\000omas'::bytea, 45)1
get_byte(string, offset)intExtract byte from string get_byte(E'Th\\000omas'::bytea, 4)109
octet_length(string)intNumber of bytes in binary stringoctet_length( E'jo\\000se'::bytea)5
position(substring in string)intLocation of specified substringposition(E'\\000om'::bytea in E'Th\\000omas'::bytea)3
set_bit(string, offset, newvalue)byteaSet bit in string set_bit(E'Th\\000omas'::bytea, 45, 0)Th\000omAs
set_byte(string, offset, newvalue)byteaSet byte in string set_byte(E'Th\\000omas'::bytea, 4, 64)Th\000o@as
substring(string [from int] [for int])byteaExtract substring substring(E'Th\\000omas'::bytea from 2 for 3)h\000o
trim([both] bytes from string)byteaRemove the longest string containing only the bytes in bytes from the start and end of stringtrim(E'\\000'::bytea from E'\\000Tom\\000'::bytea)Tom

Additional binary string manipulation functions are available and are listed in Table 9-9. Some of them are used internally to implement the SQL-standard string functions listed in Table 9-8.

Table 9-9. Other Binary String Functions

FunctionReturn TypeDescriptionExampleResult
btrim(stringbytea, bytesbytea)byteaRemove the longest string consisting only of bytes in bytes from the start and end of stringbtrim(E'\\000trim\\000'::bytea, E'\\000'::bytea)trim
decode(stringtext, typetext)byteaDecode binary string from string previously encoded with encode. Parameter type is same as in encode.decode(E'123\\000456', 'escape')123\000456
encode(stringbytea, typetext)textEncode binary string to ASCII-only representation. Supported types are: base64, hex, escape.encode(E'123\\000456'::bytea, 'escape')123\000456
length(string)intLength of binary string length(E'jo\\000se'::bytea)5
md5(string)textCalculates the MD5 hash of string, returning the result in hexadecimalmd5(E'Th\\000omas'::bytea)8ab2d3c9689aaf18 b4958c334c82d8b1
close