September 4, 2025: PostgreSQL 18 RC 1 Released!
Supported Versions: Current (17) / 16 / 15 / 14 / 13
Development Versions: 18 / 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.
PostgreSQL 7.3.21 Documentation
Prev Chapter 6. Functions and Operators Next

6.5. Binary String Functions and Operators

This section describes functions and operators for examining and manipulating binary string values. Strings in this context mean values of the 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 6-9. Some functions are also implemented using the regular syntax for function invocation. (See Table 6-10.)

Table 6-9. SQL Binary String Functions and Operators

Function Return Type Description Example Result
string || string bytea String concatenation '\\\\Post'::bytea || '\047円greSQL\000円'::bytea \\Post'greSQL000円
octet_length(string) integer Number of bytes in binary string octet_length('jo\000円se'::bytea) 5
position(substring in string) integer Location of specified substring position('\000円om'::bytea in 'Th\000円omas'::bytea) 3
substring(string [from integer] [for integer]) bytea Extract substring substring('Th\000円omas'::bytea from 2 for 3) h000円o
trim([both] characters from string) bytea Remove the longest string containing only the characters from the beginning/end/both ends of the string trim('\000円'::bytea from '\000円Tom\000円'::bytea) Tom
get_byte(string, offset) integer Extract byte from string. get_byte('Th\000円omas'::bytea, 4) 109
set_byte(string, offset, newvalue) bytea Set byte in string. set_byte('Th\000円omas'::bytea, 4, 64) Th000円o@as
get_bit(string, offset) integer Extract bit from string. get_bit('Th\000円omas'::bytea, 45) 1
set_bit(string, offset, newvalue) bytea Set bit in string. set_bit('Th\000円omas'::bytea, 45, 0) Th000円omAs

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

Table 6-10. Other Binary String Functions

Function Return Type Description Example Result
btrim(string bytea trim bytea) bytea Remove (trim) the longest string consisting only of characters in trim from the start and end of string. btrim('\000円trim\000円'::bytea,'\000円'::bytea) trim
length(string) integer Length of binary string length('jo\000円se'::bytea) 5
encode(string bytea, type text) text Encode binary string to ASCII-only representation. Supported types are: base64, hex, escape. encode('123\000456円'::bytea, 'escape') 123000456円
decode(string text, type text) bytea Decode binary string from string previously encoded with encode(). Parameter type is same as in encode(). decode('123\000456円', 'escape') 123000456円

Prev Home Next
String Functions and Operators Up Pattern Matching

AltStyle によって変換されたページ (->オリジナル) /