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 |
Prev |
Up |
Chapter 9. Functions and Operators |
Home |
Next |
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 that use key words, rather than commas, to separate arguments. Details are in Table 9.11. PostgreSQL also provides versions of these functions that use the regular function invocation syntax (see Table 9.12).
Note
The sample results shown on this page assume that the server parameter bytea_output
is set to escape
(the traditional PostgreSQL format).
Table 9.11. SQL Binary String Functions and Operators
Function |
Return Type |
Description |
Example |
Result |
string || string
|
bytea |
String concatenation |
'\\Post'::bytea || '047円gres000円'::bytea |
\\Post'gres000円 |
octet_length(string )
|
int |
Number of bytes in binary string |
octet_length('jo000円se'::bytea) |
5 |
overlay(string placing string from int [for int ])
|
bytea |
Replace substring |
overlay('Th000円omas'::bytea placing '002円003円'::bytea from 2 for 3) |
T\002円\003円mas |
position(substring in string )
|
int |
Location of specified substring |
position('000円om'::bytea in 'Th000円omas'::bytea) |
3 |
substring(string [from int ] [for int ])
|
bytea |
Extract substring |
substring('Th000円omas'::bytea from 2 for 3) |
h000円o |
trim([both] bytes from string )
|
bytea |
Remove the longest string containing only bytes appearing in bytes from the start and end of string |
trim('000円001円'::bytea from '000円Tom001円'::bytea) |
Tom |
Additional binary string manipulation functions are available and are listed in Table 9.12. Some of them are used internally to implement the SQL-standard string functions listed in Table 9.11.
Table 9.12. Other Binary String Functions
Function |
Return Type |
Description |
Example |
Result |
btrim(string bytea , bytes bytea )
|
bytea |
Remove the longest string containing only bytes appearing in bytes from the start and end of string |
btrim('000円trim001円'::bytea, '000円001円'::bytea) |
trim |
decode(string text , format text )
|
bytea |
Decode binary data from textual representation in string . Options for format are same as in encode . |
decode('123000456円', 'escape') |
123000456円 |
encode(data bytea , format text )
|
text |
Encode binary data into a textual representation. Supported formats are: base64 , hex , escape . escape converts zero bytes and high-bit-set bytes to octal sequences (\ nnn ) and doubles backslashes. |
encode('123000456円'::bytea, 'escape') |
123000456円 |
get_bit(string , offset )
|
int |
Extract bit from string |
get_bit('Th000円omas'::bytea, 45) |
1 |
get_byte(string , offset )
|
int |
Extract byte from string |
get_byte('Th000円omas'::bytea, 4) |
109 |
length(string )
|
int |
Length of binary string |
length('jo000円se'::bytea) |
5 |
md5(string )
|
text |
Calculates the MD5 hash of string , returning the result in hexadecimal |
md5('Th000円omas'::bytea) |
8ab2d3c9689aaf18 b4958c334c82d8b1 |
set_bit(string , offset , newvalue )
|
bytea |
Set bit in string |
set_bit('Th000円omas'::bytea, 45, 0) |
Th000円omAs |
set_byte(string , offset , newvalue )
|
bytea |
Set byte in string |
set_byte('Th000円omas'::bytea, 4, 64) |
Th000円o@as |
get_byte
and set_byte
number the first byte of a binary string as byte 0. get_bit
and set_bit
number bits from the right within each byte; for example bit 0 is the least significant bit of the first byte, and bit 15 is the most significant bit of the second byte.
See also the aggregate function string_agg
in Section 9.20 and the large object functions in Section 34.4.
Prev |
Up |
Next |
9.4. String Functions and Operators |
Home |
9.6. Bit String Functions and Operators |