9.5. 位元字串函式及運算子

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

SQLdefines some string functions that use key words, rather than commas, to separate arguments. Details are inTable 9.11.PostgreSQLalso provides versions of these functions that use the regular function invocation syntax (seeTable 9.12).

Note

The sample results shown on this page assume that the server parameterbytea_outputis set toescape(the traditional PostgreSQL format).

Table 9.11. SQLBinary String Functions and Operators

Function

Return Type

Description

Example

Result

string`

_string`_

bytea

String concatenation

`E'\\Post'::bytea

E'\047gres\000'::bytea`

\\Post'gres\000

octet_length(string)

int

Number of bytes in binary string

octet_length(E'jo\\000se'::bytea)

5

overlay(string_placingstring_fromint[forint])

bytea

Replace substring

overlay(E'Th\\000omas'::bytea placing E'\\002\\003'::bytea from 2 for 3)

T\\002\\003mas

position(substring_instring_)

int

Location of specified substring

position(E'\\000om'::bytea in E'Th\\000omas'::bytea)

3

substring(string[fromint] [forint])

bytea

Extract substring

substring(E'Th\\000omas'::bytea from 2 for 3)

h\000o

trim([both]bytes_fromstring_)

bytea

Remove the longest string containing only bytes appearing inbytes_from the start and end ofstring_

trim(E'\\000\\001'::bytea from E'\\000Tom\\001'::bytea)

Tom

Additional binary string manipulation functions are available and are listed inTable 9.12. Some of them are used internally to implement theSQL-standard string functions listed inTable 9.11.

Table 9.12. Other Binary String Functions

Function

Return Type

Description

Example

Result

btrim(stringbytea,bytesbytea)

bytea

Remove the longest string containing only bytes appearing inbytes_from the start and end ofstring_

btrim(E'\\000trim\\001'::bytea, E'\\000\\001'::bytea)

trim

decode(stringtext,formattext)

bytea

Decode binary data from textual representation instring. Options for_format_are same as inencode.

decode(E'123\\000456', 'escape')

123\000456

encode(databytea,formattext)

text

Encode binary data into a textual representation. Supported formats are:base64,hex,escape.escapeconverts zero bytes and high-bit-set bytes to octal sequences (\nnn) and doubles backslashes.

encode(E'123\\000456'::bytea, 'escape')

123\000456

get_bit(string,offset)

int

Extract bit from string

get_bit(E'Th\\000omas'::bytea, 45)

1

get_byte(string,offset)

int

Extract byte from string

get_byte(E'Th\\000omas'::bytea, 4)

109

length(string)

int

Length of binary string

length(E'jo\\000se'::bytea)

5

md5(string)

text

Calculates the MD5 hash ofstring, returning the result in hexadecimal

md5(E'Th\\000omas'::bytea)

8ab2d3c9689aaf18 b4958c334c82d8b1

set_bit(string,offset,newvalue)

bytea

Set bit in string

set_bit(E'Th\\000omas'::bytea, 45, 0)

Th\000omAs

set_byte(string,offset,newvalue)

bytea

Set byte in string

set_byte(E'Th\\000omas'::bytea, 4, 64)

Th\000o@as

get_byteandset_bytenumber the first byte of a binary string as byte 0.get_bitandset_bitnumber 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 functionstring_agginSection 9.20and the large object functions inSection 34.4.

Last updated