2 9.5. Binary String Functions and Operators #
4 This section describes functions and operators for examining and
5 manipulating binary strings, that is values of type bytea. Many of
6 these are equivalent, in purpose and syntax, to the text-string
7 functions described in the previous section.
9 SQL defines some string functions that use key words, rather than
10 commas, to separate arguments. Details are in Table 9.11. PostgreSQL
11 also provides versions of these functions that use the regular function
12 invocation syntax (see Table 9.12).
14 Table 9.11. SQL Binary String Functions and Operators
22 bytea || bytea → bytea
24 Concatenates the two binary strings.
26 '\x123456'::bytea || '\x789a00bcde'::bytea → \x123456789a00bcde
28 bit_length ( bytea ) → integer
30 Returns number of bits in the binary string (8 times the octet_length).
32 bit_length('\x123456'::bytea) → 24
34 btrim ( bytes bytea, bytesremoved bytea ) → bytea
36 Removes the longest string containing only bytes appearing in
37 bytesremoved from the start and end of bytes.
39 btrim('\x1234567890'::bytea, '\x9012'::bytea) → \x345678
41 ltrim ( bytes bytea, bytesremoved bytea ) → bytea
43 Removes the longest string containing only bytes appearing in
44 bytesremoved from the start of bytes.
46 ltrim('\x1234567890'::bytea, '\x9012'::bytea) → \x34567890
48 octet_length ( bytea ) → integer
50 Returns number of bytes in the binary string.
52 octet_length('\x123456'::bytea) → 3
54 overlay ( bytes bytea PLACING newsubstring bytea FROM start integer [
55 FOR count integer ] ) → bytea
57 Replaces the substring of bytes that starts at the start'th byte and
58 extends for count bytes with newsubstring. If count is omitted, it
59 defaults to the length of newsubstring.
61 overlay('\x1234567890'::bytea placing '\002\003'::bytea from 2 for 3) →
64 position ( substring bytea IN bytes bytea ) → integer
66 Returns first starting index of the specified substring within bytes,
67 or zero if it's not present.
69 position('\x5678'::bytea in '\x1234567890'::bytea) → 3
71 rtrim ( bytes bytea, bytesremoved bytea ) → bytea
73 Removes the longest string containing only bytes appearing in
74 bytesremoved from the end of bytes.
76 rtrim('\x1234567890'::bytea, '\x9012'::bytea) → \x12345678
78 substring ( bytes bytea [ FROM start integer ] [ FOR count integer ] )
81 Extracts the substring of bytes starting at the start'th byte if that
82 is specified, and stopping after count bytes if that is specified.
83 Provide at least one of start and count.
85 substring('\x1234567890'::bytea from 3 for 2) → \x5678
87 trim ( [ LEADING | TRAILING | BOTH ] bytesremoved bytea FROM bytes
90 Removes the longest string containing only bytes appearing in
91 bytesremoved from the start, end, or both ends (BOTH is the default) of
94 trim('\x9012'::bytea from '\x1234567890'::bytea) → \x345678
96 trim ( [ LEADING | TRAILING | BOTH ] [ FROM ] bytes bytea, bytesremoved
99 This is a non-standard syntax for trim().
101 trim(both from '\x1234567890'::bytea, '\x9012'::bytea) → \x345678
103 Additional binary string manipulation functions are available and are
104 listed in Table 9.12. Some of them are used internally to implement the
105 SQL-standard string functions listed in Table 9.11.
107 Table 9.12. Other Binary String Functions
115 bit_count ( bytes bytea ) → bigint
117 Returns the number of bits set in the binary string (also known as
120 bit_count('\x1234567890'::bytea) → 15
122 crc32 ( bytea ) → bigint
124 Computes the CRC-32 value of the binary string.
126 crc32('abc'::bytea) → 891568578
128 crc32c ( bytea ) → bigint
130 Computes the CRC-32C value of the binary string.
132 crc32c('abc'::bytea) → 910901175
134 get_bit ( bytes bytea, n bigint ) → integer
136 Extracts n'th bit from binary string.
138 get_bit('\x1234567890'::bytea, 30) → 1
140 get_byte ( bytes bytea, n integer ) → integer
142 Extracts n'th byte from binary string.
144 get_byte('\x1234567890'::bytea, 4) → 144
146 length ( bytea ) → integer
148 Returns the number of bytes in the binary string.
150 length('\x1234567890'::bytea) → 5
152 length ( bytes bytea, encoding name ) → integer
154 Returns the number of characters in the binary string, assuming that it
155 is text in the given encoding.
157 length('jose'::bytea, 'UTF8') → 4
161 Computes the MD5 hash of the binary string, with the result written in
164 md5('Th\000omas'::bytea) → 8ab2d3c9689aaf18b4958c334c82d8b1
166 reverse ( bytea ) → bytea
168 Reverses the order of the bytes in the binary string.
170 reverse('\xabcd'::bytea) → \xcdab
172 set_bit ( bytes bytea, n bigint, newvalue integer ) → bytea
174 Sets n'th bit in binary string to newvalue.
176 set_bit('\x1234567890'::bytea, 30, 0) → \x1234563890
178 set_byte ( bytes bytea, n integer, newvalue integer ) → bytea
180 Sets n'th byte in binary string to newvalue.
182 set_byte('\x1234567890'::bytea, 4, 64) → \x1234567840
184 sha224 ( bytea ) → bytea
186 Computes the SHA-224 hash of the binary string.
188 sha224('abc'::bytea) →
189 \x23097d223405d8228642a477bda255b32aadbce4bda0b3f7e36c9da7
191 sha256 ( bytea ) → bytea
193 Computes the SHA-256 hash of the binary string.
195 sha256('abc'::bytea) →
196 \xba7816bf8f01cfea414140de5dae2223b00361a396177a9cb410ff61f20015ad
198 sha384 ( bytea ) → bytea
200 Computes the SHA-384 hash of the binary string.
202 sha384('abc'::bytea) →
203 \xcb00753f45a35e8bb5a03d699ac65007272c32ab0eded1631a8b605a43ff5bed808
204 6072ba1e7cc2358baeca134c825a7
206 sha512 ( bytea ) → bytea
208 Computes the SHA-512 hash of the binary string.
210 sha512('abc'::bytea) →
211 \xddaf35a193617abacc417349ae20413112e6fa4e89a97ea20a9eeee64b55d39a219
212 2992a274fc1a836ba3c23a3feebbd454d4423643ce80e2a9ac94fa54ca49f
214 substr ( bytes bytea, start integer [, count integer ] ) → bytea
216 Extracts the substring of bytes starting at the start'th byte, and
217 extending for count bytes if that is specified. (Same as
218 substring(bytes from start for count).)
220 substr('\x1234567890'::bytea, 3, 2) → \x5678
222 Functions get_byte and set_byte number the first byte of a binary
223 string as byte 0. Functions get_bit and set_bit number bits from the
224 right within each byte; for example bit 0 is the least significant bit
225 of the first byte, and bit 15 is the most significant bit of the second
228 For historical reasons, the function md5 returns a hex-encoded value of
229 type text whereas the SHA-2 functions return type bytea. Use the
230 functions encode and decode to convert between the two. For example
231 write encode(sha256('abc'), 'hex') to get a hex-encoded text
232 representation, or decode(md5('abc'), 'hex') to get a bytea value.
234 Functions for converting strings between different character sets
235 (encodings), and for representing arbitrary binary data in textual
236 form, are shown in Table 9.13. For these functions, an argument or
237 result of type text is expressed in the database's default encoding,
238 while arguments or results of type bytea are in an encoding named by
241 Table 9.13. Text/Binary String Conversion Functions
249 convert ( bytes bytea, src_encoding name, dest_encoding name ) → bytea
251 Converts a binary string representing text in encoding src_encoding to
252 a binary string in encoding dest_encoding (see Section 23.3.4 for
253 available conversions).
255 convert('text_in_utf8', 'UTF8', 'LATIN1') → \x746578745f696e5f75746638
257 convert_from ( bytes bytea, src_encoding name ) → text
259 Converts a binary string representing text in encoding src_encoding to
260 text in the database encoding (see Section 23.3.4 for available
263 convert_from('text_in_utf8', 'UTF8') → text_in_utf8
265 convert_to ( string text, dest_encoding name ) → bytea
267 Converts a text string (in the database encoding) to a binary string
268 encoded in encoding dest_encoding (see Section 23.3.4 for available
271 convert_to('some_text', 'UTF8') → \x736f6d655f74657874
273 encode ( bytes bytea, format text ) → text
275 Encodes binary data into a textual representation; supported format
276 values are: base64, escape, hex.
278 encode('123\000\001', 'base64') → MTIzAAE=
280 decode ( string text, format text ) → bytea
282 Decodes binary data from a textual representation; supported format
283 values are the same as for encode.
285 decode('MTIzAAE=', 'base64') → \x3132330001
287 The encode and decode functions support the following textual formats:
290 The base64 format is that of RFC 2045 Section 6.8. As per the
291 RFC, encoded lines are broken at 76 characters. However instead
292 of the MIME CRLF end-of-line marker, only a newline is used for
293 end-of-line. The decode function ignores carriage-return,
294 newline, space, and tab characters. Otherwise, an error is
295 raised when decode is supplied invalid base64 data — including
296 when trailing padding is incorrect.
299 The escape format converts zero bytes and bytes with the high
300 bit set into octal escape sequences (\nnn), and it doubles
301 backslashes. Other byte values are represented literally. The
302 decode function will raise an error if a backslash is not
303 followed by either a second backslash or three octal digits; it
304 accepts other byte values unchanged.
307 The hex format represents each 4 bits of data as one hexadecimal
308 digit, 0 through f, writing the higher-order digit of each byte
309 first. The encode function outputs the a-f hex digits in lower
310 case. Because the smallest unit of data is 8 bits, there are
311 always an even number of characters returned by encode. The
312 decode function accepts the a-f characters in either upper or
313 lower case. An error is raised when decode is given invalid hex
314 data — including when given an odd number of characters.
316 In addition, it is possible to cast integral values to and from type
317 bytea. Casting an integer to bytea produces 2, 4, or 8 bytes, depending
318 on the width of the integer type. The result is the two's complement
319 representation of the integer, with the most significant byte first.
321 1234::smallint::bytea \x04d2
322 cast(1234 as bytea) \x000004d2
323 cast(-1234 as bytea) \xfffffb2e
324 '\x8000'::bytea::smallint -32768
325 '\x8000'::bytea::integer 32768
327 Casting a bytea to an integer will raise an error if the length of the
328 bytea exceeds the width of the integer type.
330 See also the aggregate function string_agg in Section 9.21 and the
331 large object functions in Section 33.4.