Blob and bitstring functions in DuckDB
Use blob and bitstring functions to work with binary data. A BLOB value holds a sequence of bytes, and a BIT value, which you can also write as BITSTRING, holds a sequence of bits. This category also has the hash functions md5, md5_number, md5_number_lower, md5_number_upper, sha1, and sha256. MD5 and SHA-1 are not secure for cryptographic use, so do not use the MD5 and SHA-1 functions to protect data. When you need a secure hash, use the sha256 function.
The category also has the repeat function, which repeats strings, blobs, and lists. To read files as binary data, use the read_blob table function. For more information about read_blob, see read_blob function.
To write a BLOB literal, cast a string to BLOB, as in '\xAA\xBB'::BLOB. Each \x followed by two hexadecimal digits stands for one byte, and each other ASCII character stands for its own byte. A cast to BLOB fails for a string that holds a character outside the ASCII range, such as chr(233). To get the UTF-8 bytes of such a string, use encode instead. To write a BIT literal, cast a string of 0 and 1 characters, as in '1011'::BIT. To convert text to the bytes of its UTF-8 encoding and back, use the encode function and the decode function. To convert a BLOB to or from base64 text, use the to_base64 function and the from_base64 function. To write a value as hexadecimal or binary digits, use the hex function or the bin function. To turn such digits back into a BLOB, use the unhex function or the unbin function.
Query results show a BLOB value as text. Bytes that are letters, digits, and most other printable ASCII characters appear as those characters, and other bytes appear as \x followed by two hexadecimal digits, for example \xAA. A BIT value appears as a string of 0 and 1 characters.
Hive and Iceberg tables cannot store BIT values. To store a bitstring in a table, cast it to VARCHAR, as in CAST(.bits AS VARCHAR)
The following table lists the blob and bitstring functions, with a link to the description of each one.
| Function | Description |
|---|---|
base64 |
Alias of to_base64. |
Returns the binary digits of an integer, or of the bytes of a string, as a string. |
|
Counts the |
|
Returns the number of bits in a string or a bitstring. |
|
Returns the position of the first occurrence of one bitstring inside another. |
|
Returns a bitstring widened to a given number of bits, with |
|
Reads the bytes of a |
|
Returns the UTF-8 bytes of a string as a |
|
Decodes a base64-encoded string and returns the bytes as a |
|
from_binary |
Alias of unbin. |
from_hex |
Alias of unhex. |
Returns the bit at a given position in a bitstring. |
|
Returns a 64-bit hash of one or more values of any type. It is not a cryptographic hash. The utility functions topic describes it. |
|
Converts an integer, a string, or a blob to hexadecimal text. |
|
Hashes a string or a blob with MD5 and returns the hash as hexadecimal text. |
|
Hashes a string or a blob with MD5 and returns the hash as an unsigned 128-bit integer. |
|
Returns the most significant 64 bits of the 128-bit MD5 hash number of a value. |
|
Returns the least significant 64 bits of the 128-bit MD5 hash number of a value. |
|
Returns the size of a blob or a bitstring in bytes. |
|
Joins a given number of copies of a string, a blob, or a list into one value, or returns a value in a given number of rows. |
|
Returns a copy of a bitstring with the bit at a given position set to |
|
Hashes a string or a blob with SHA-1 and returns the hash as hexadecimal text. |
|
Hashes a string or a blob with SHA-256 and returns the hash as hexadecimal text. |
|
Encodes the bytes of a |
|
to_binary |
Alias of bin. |
to_hex |
Alias of hex. |
Decodes a string of binary digits and returns the bytes as a |
|
Decodes a string of hexadecimal digits and returns the bytes as a |