Movatterモバイル変換


[0]ホーム

URL:



Facebook
Postgres Pro
Facebook
Downloads
9.5. Binary String Functions and Operators
Prev UpChapter 9. Functions and OperatorsHome Next

9.5. Binary String Functions and Operators

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

SQL defines some string functions that use key words, rather than commas, to separate arguments. Details are inTable 9.11.Postgres Pro also 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_output is set toescape (the traditional Postgres Pro format).

Table 9.11. SQL Binary String Functions and Operators

FunctionReturn TypeDescriptionExampleResult
string||stringbytea String concatenation'\\Post'::bytea || '\047gres\000'::bytea\\Post'gres\000
octet_length(string)intNumber of bytes in binary stringoctet_length('jo\000se'::bytea)5
overlay(string placingstring fromint [forint])bytea Replace substringoverlay('Th\000omas'::bytea placing '\002\003'::bytea from 2 for 3)T\\002\\003mas
position(substring instring)intLocation of specified substringposition('\000om'::bytea in 'Th\000omas'::bytea)3
substring(string [fromint] [forint])bytea Extract substringsubstring('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 ofstringtrim('\000\001'::bytea from '\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

FunctionReturn TypeDescriptionExampleResult
btrim(stringbytea,bytesbytea)bytea Remove the longest string containing only bytes appearing inbytes from the start and end ofstringbtrim('\000trim\001'::bytea, '\000\001'::bytea)trim
decode(stringtext,formattext)bytea Decode binary data from textual representation instring. Options forformat are same as inencode.decode('123\000456', 'escape')123\000456
encode(databytea,formattext)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('123\000456'::bytea, 'escape')123\000456
get_bit(string,offset)int Extract bit from stringget_bit('Th\000omas'::bytea, 45)1
get_byte(string,offset)int Extract byte from stringget_byte('Th\000omas'::bytea, 4)109
length(string)int Length of binary stringlength('jo\000se'::bytea)5
md5(string)text Calculates the MD5 hash ofstring, returning the result in hexadecimalmd5('Th\000omas'::bytea)8ab2d3c9689aaf18​b4958c334c82d8b1
set_bit(string,offset,newvalue)bytea Set bit in stringset_bit('Th\000omas'::bytea, 45, 0)Th\000omAs
set_byte(string,offset,newvalue)bytea Set byte in stringset_byte('Th\000omas'::bytea, 4, 64)Th\000o@as
sha224(bytea)bytea SHA-224 hashsha224('abc')\x23097d223405d8228642a477bda2​55b32aadbce4bda0b3f7e36c9da7
sha256(bytea)bytea SHA-256 hashsha256('abc')\xba7816bf8f01cfea414140de5dae2223​b00361a396177a9cb410ff61f20015ad
sha384(bytea)bytea SHA-384 hashsha384('abc')\xcb00753f45a35e8bb5a03d699ac65007​272c32ab0eded1631a8b605a43ff5bed​8086072ba1e7cc2358baeca134c825a7
sha512(bytea)bytea SHA-512 hashsha512('abc')\xddaf35a193617abacc417349ae204131​12e6fa4e89a97ea20a9eeee64b55d39a​2192992a274fc1a836ba3c23a3feebbd​454d4423643ce80e2a9ac94fa54ca49f

get_byte andset_byte number the first byte of a binary string as byte 0.get_bit andset_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.

Note that for historic reasons, the functionmd5 returns a hex-encoded value of typetext whereas the SHA-2 functions return typebytea. Use the functionsencode anddecode to convert between the two, for exampleencode(sha256('abc'), 'hex') to get a hex-encoded text representation.

See also the aggregate functionstring_agg inSection 9.20 and the large object functions inSection 33.4.


Prev Up Next
9.4. String Functions and Operators Home 9.6. Bit String Functions and Operators
epubpdf
Go to Postgres Pro Standard 11
By continuing to browse this website, you agree to the use of cookies. Go toPrivacy Policy.

[8]ページ先頭

©2009-2025 Movatter.jp