PostgreSQL
9.5. Binary String Functions and Operators
This section describes functions and operators for examining and manipulating binary strings, that is values of type bytea
. Many of these are equivalent, in purpose and syntax, to the text-string functions described in the previous section.
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).
Table 9.11. SQL Binary String Functions and Operators
Function/Operator Description Example(s) |
---|
`+bytea+` `+ |
` `+bytea` → Concatenates the two binary strings. `+'\x123456'::bytea |
'\x789a00bcde'::bytea+` → |
`+bit_length+` ( `+bytea+` ) → `+integer+` Returns number of bits in the binary string (8 times the
|
`+octet_length+` ( `+bytea+` ) → `+integer+` Returns number of bytes in the binary string.
|
`+overlay+` ( _`+bytes+`_ `+bytea+` `+PLACING+` _`+newsubstring+`_ `+bytea+` `+FROM+` _`+start+`_ `+integer+` [ [.optional]#`+FOR+` _`+count+`_ `+integer+`# ] ) → `+bytea+` Replaces the substring of `bytes
|
`+position+` ( _`+substring+`_ `+bytea+` `+IN+` _`+bytes+`_ `+bytea+` ) → `+integer+` Returns first starting index of the specified `substring
|
`+substring+` ( _`+bytes+`_ `+bytea+` [ [.optional]#`+FROM+` _`+start+`_ `+integer+`# ] [ [.optional]#`+FOR+` _`+count+`_ `+integer+`# ] ) → `+bytea+` Extracts the substring of `bytes
|
`+trim+` ( [ [.optional]#`+LEADING+` | `+TRAILING+` | `+BOTH+`# ] _`+bytesremoved+`_ `+bytea+` `+FROM+` _`+bytes+`_ `+bytea+` ) → `+bytea+` Removes the longest string containing only bytes appearing in `bytesremoved
|
This is a non-standard syntax for
|
+
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 Description Example(s) |
---|
`+bit_count+` ( _`+bytes+`_ `+bytea+` ) → `+bigint+` Returns the number of bits set in the binary string (also known as “[.quote]#popcount”#).
|
`+btrim+` ( _`+bytes+`_ `+bytea+`, _`+bytesremoved+`_ `+bytea+` ) → `+bytea+` Removes the longest string containing only bytes appearing in `bytesremoved
|
`+get_bit+` ( _`+bytes+`_ `+bytea+`, _`+n+`_ `+bigint+` ) → `+integer+` Extracts n’th bit from binary string.
|
`+get_byte+` ( _`+bytes+`_ `+bytea+`, _`+n+`_ `+integer+` ) → `+integer+` Extracts n’th byte from binary string.
|
`+length+` ( `+bytea+` ) → `+integer+` Returns the number of bytes in the binary string.
|
Returns the number of characters in the binary string, assuming that it is text in the given `encoding`.
|
`+ltrim+` ( _`+bytes+`_ `+bytea+`, _`+bytesremoved+`_ `+bytea+` ) → `+bytea+` Removes the longest string containing only bytes appearing in `bytesremoved
|
`+md5+` ( `+bytea+` ) → `+text+` Computes the MD5 hash of the binary string, with the result written in hexadecimal.
|
`+rtrim+` ( _`+bytes+`_ `+bytea+`, _`+bytesremoved+`_ `+bytea+` ) → `+bytea+` Removes the longest string containing only bytes appearing in `bytesremoved
|
`+set_bit+` ( _`+bytes+`_ `+bytea+`, _`+n+`_ `+bigint+`, _`+newvalue+`_ `+integer+` ) → `+bytea+` Sets n’th bit in binary string to `newvalue`.
|
`+set_byte+` ( _`+bytes+`_ `+bytea+`, _`+n+`_ `+integer+`, _`+newvalue+`_ `+integer+` ) → `+bytea+` Sets n’th byte in binary string to `newvalue`.
|
`+sha224+` ( `+bytea+` ) → `+bytea+` Computes the SHA-224 hash of the binary string.
|
`+sha256+` ( `+bytea+` ) → `+bytea+` Computes the SHA-256 hash of the binary string.
|
`+sha384+` ( `+bytea+` ) → `+bytea+` Computes the SHA-384 hash of the binary string.
|
`+sha512+` ( `+bytea+` ) → `+bytea+` Computes the SHA-512 hash of the binary string.
|
`+substr+` ( _`+bytes+`_ `+bytea+`, _`+start+`_ `+integer+` [[.optional]#, _`+count+`_ `+integer+`# ] ) → `+bytea+` Extracts the substring of `bytes
|
+
Functions get_byte
and set_byte
number the first byte of a binary string as byte 0. Functions 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.
For historical reasons, the function md5
returns a hex-encoded value of type text
whereas the SHA-2 functions return type bytea
. Use the functions encode
and decode
to convert between the two. For example write encode(sha256('abc'), 'hex')
to get a hex-encoded text representation, or decode(md5('abc'), 'hex')
to get a bytea
value.
Functions for converting strings between different character sets (encodings), and for representing arbitrary binary data in textual form, are shown in link:functions-binarystring.html#FUNCTIONS-BINARYSTRING-CONVERSIONS[Table 9.13]. For these functions, an argument or result of type `+text+` is expressed in the database's default encoding, while arguments or results of type `+bytea+` are in an encoding named by another argument.
Table 9.13. Text/Binary String Conversion Functions
Function Description Example(s) |
---|
`+convert+` ( _`+bytes+`_ `+bytea+`, _`+src_encoding+`_ `+name+`, _`+dest_encoding+`_ `+name+` ) → `+bytea+` Converts a binary string representing text in encoding `src_encoding
|
`+convert_from+` ( _`+bytes+`_ `+bytea+`, _`+src_encoding+`_ `+name+` ) → `+text+` Converts a binary string representing text in encoding `src_encoding
|
`+convert_to+` ( _`+string+`_ `+text+`, _`+dest_encoding+`_ `+name+` ) → `+bytea+` Converts a
|
[FUNCTION-DECODE .indexterm]# Decodes binary data from a textual representation; supported `format
|
+
The encode
and decode
functions support the following textual formats:
- [.term]#base64
-
The
base64
format is that of RFC 2045 Section 6.8. As per the RFC, encoded lines are broken at 76 characters. However instead of the MIME CRLF end-of-line marker, only a newline is used for end-of-line. Thedecode
function ignores carriage-return, newline, space, and tab characters. Otherwise, an error is raised whendecode
is supplied invalid base64 data — including when trailing padding is incorrect. - [.term]#escape
-
The
escape
format converts zero bytes and bytes with the high bit set into octal escape sequences (\`
nnn), and it doubles backslashes. Other byte values are represented literally. The `decode
function will raise an error if a backslash is not followed by either a second backslash or three octal digits; it accepts other byte values unchanged. - [.term]#hex
-
The
hex
format represents each 4 bits of data as one hexadecimal digit,0
throughf
, writing the higher-order digit of each byte first. Theencode
function outputs thea
-f
hex digits in lower case. Because the smallest unit of data is 8 bits, there are always an even number of characters returned byencode
. Thedecode
function accepts thea
-f
characters in either upper or lower case. An error is raised whendecode
is given invalid hex data — including when given an odd number of characters.
See also the aggregate function string_agg
in Section 9.21 and the large object functions in Section 35.4.
Prev | Up | Next |
---|---|---|
9.4. String Functions and Operators |
9.6. Bit String Functions and Operators |
Submit correction
If you see anything in the documentation that is not correct, does not match your experience with the particular feature or requires further clarification, please use this form to report a documentation issue.
Copyright © 1996-2023 The PostgreSQL Global Development Group