Documentation
 
 
 

8.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 available in Table 8-8. Some functions are also implemented using the regular syntax for function invocation. (See Table 8-9.)

Table 8-8. SQL Binary String Functions and Operators

FunctionReturn TypeDescriptionExampleResult
string || string BYTEA String concatenation '\\\\Enterprise'::bytea || '\\047DB\\000'::bytea\\Enterprise'DB\000
octet_length(string)INTEGERNumber of bytes in binary stringoctet_length( 'jo\\000se'::bytea)5
position(substring in string)INTEGERLocation of specified substringposition('\\000om'::bytea in 'Th\\000omas'::bytea)3
substring(string [from INTEGER] [for INTEGER])BYTEA Extract substring substring('Th\\000omas'::bytea from 2 for 3)h\000o
trim([both] bytes from string) BYTEA Remove the longest string containing only the bytes in bytes from the start and end of string trim('\\000'::bytea from '\\000Tom\\000'::bytea)Tom
get_byte(string, offset)INTEGER Extract byte from string. get_byte('Th\\000omas'::bytea, 4)109
set_byte(string, offset, newvalue)BYTEA Set byte in string. set_byte('Th\\000omas'::bytea, 4, 64)Th\000o@as
get_bit(string, offset)INTEGER Extract bit from string. get_bit('Th\\000omas'::bytea, 45)1
set_bit(string, offset, newvalue)BYTEA Set bit in string. set_bit('Th\\000omas'::bytea, 45, 0)Th\000omAs

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

Table 8-9. Other Binary String Functions

FunctionReturn TypeDescriptionExampleResult
btrim(string BYTEA, bytes BYTEA)BYTEA Remove the longest string consisting only of bytes in bytes from the start and end of string. btrim('\\000trim\\000'::bytea, '\\000'::bytea)trim
length(string)INTEGER Length of binary string length('jo\\000se'::bytea)5
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')123\000456
encode(string BYTEA, type TEXT) TEXT Encode binary string to ASCII-only representation. Supported types are: base64, hex, escape. encode('123\\000456'::bytea, 'escape')123\000456
 
 ©2004-2007 EnterpriseDB All Rights Reserved