Blob Functions
This section describes functions and operators for examining and manipulating BLOB values.
| Function | Description |
|---|---|
arg1 || arg2 | Concatenates two strings, lists, or blobs. A NULL string or blob makes the result NULL; a NULL list counts as an empty one, so a list result is NULL only when every input is NULL. See also concat(arg1, arg2, ...) and list_concat(list1, list2, ...). |
base64(blob) | Alias for to_base64. |
bit_length(blob) | Number of bits in blob. |
btrim(blob, bytes) | Removes the bytes that occur in bytes from both ends of blob. |
decode(blob[, on_error]) | Converts blob to VARCHAR. The optional on_error parameter controls handling of invalid UTF-8: 'strict' (default, throws error), 'replace' (replaces invalid characters with ?), or 'ignore' (removes invalid characters). |
encode(string) | Converts the string to BLOB. Converts UTF-8 characters into literal encoding. |
from_base64(string) | Converts a base64 encoded string to a character string (BLOB). |
from_binary(value) | Alias for unbin. |
from_hex(value) | Alias for unhex. |
hex(blob) | Converts blob to VARCHAR using hexadecimal encoding. |
length(blob) | Alias for octet_length. |
ltrim(blob, bytes) | Removes the bytes that occur in bytes from the start of blob. |
md5(blob) | Returns the MD5 hash of the blob as a VARCHAR. |
md5_number(blob) | Returns the MD5 hash of the blob as a HUGEINT. |
octet_length(blob) | Number of bytes in blob. |
overlay(blob PLACING replacement FROM start [FOR count]) | Replaces count bytes of blob, from byte start on, with replacement. |
position(search IN blob) | Returns the byte position of the first occurrence of search in blob, counting from 1, or 0 if no match is found. |
read_blob(source) | Returns the content from source (a filename, a list of filenames, or a glob pattern) as a BLOB. See the read_blob guide for more details. |
repeat(blob, count) | Repeats the blob count number of times. |
rtrim(blob, bytes) | Removes the bytes that occur in bytes from the end of blob. |
sha1(blob) | Returns a VARCHAR with the SHA-1 hash of the blob. |
sha256(blob) | Returns a VARCHAR with the SHA-256 hash of the blob. |
substr(blob, start[, length]) | Alias for substring. |
substring(blob, start[, length]) | Extracts length bytes of blob from byte start on, or every byte from start to the end. |
to_base64(blob) | Converts a blob to a base64 encoded string. |
to_hex(blob) | Alias for hex. |
trim(blob, bytes) | Alias for btrim. |
unbin(value) | Converts a value from binary representation to a blob. |
unhex(value) | Converts a value from hexadecimal representation to a blob. |
arg1 || arg2
Concatenates two strings, lists, or blobs. A NULL string or blob makes the result NULL; a NULL list counts as an empty one, so a list result is NULL only when every input is NULL. See also concat(arg1, arg2, ...) and list_concat(list1, list2, ...).
SELECT 'Serene' || 'DB' AS string_concat, [1, 2, 3] || [4, 5, 6] AS list_concat, '\xAA'::BLOB || '\xBB'::BLOB AS blob_concat; string_concat | list_concat | blob_concat
---------------+---------------+-------------
SereneDB | {1,2,3,4,5,6} | \\xaabbbit_length(blob)
Number of bits in blob: eight times its number of bytes.
SELECT bit_length('\xAABB'::BLOB) AS bit_length; bit_length
------------
16btrim(blob, bytes)
Removes the bytes that occur in bytes from both ends of blob. ltrim removes them from the start only, rtrim from the end only, and trim([LEADING | TRAILING | BOTH] bytes FROM blob) from the ends it names, both by default.
SELECT btrim('\x00AA00'::BLOB, '\x00'::BLOB) AS btrim, ltrim('\x00AA00'::BLOB, '\x00'::BLOB) AS ltrim, rtrim('\x00AA00'::BLOB, '\x00'::BLOB) AS rtrim, trim(LEADING '\x00'::BLOB FROM '\x00AA00'::BLOB) AS trim_leading;btrim | ltrim | rtrim | trim_leading -------+---------+---------+-------------- \\xaa | \\xaa00 | \\x00aa | \\xaa00
decode(blob[, on_error])
Converts blob to VARCHAR. The optional on_error parameter controls handling of invalid UTF-8: 'strict' (default, throws error), 'replace' (replaces invalid characters with ?), or 'ignore' (removes invalid characters).
SELECT decode('\xC3BC'::BLOB) AS decode, decode('\xAA'::BLOB, 'replace') AS decode_replace;decode | decode_replace --------+---------------- ü | ?
encode(string)
Converts the string to BLOB. Converts UTF-8 characters into literal encoding.
SELECT encode('my_string_with_ü') AS encode;encode --------------------------------------- \\x6d795f737472696e675f776974685fc3bc
from_base64(string)
Converts a base64 encoded string to a character string (BLOB).
SELECT from_base64('QQ==') AS from_base64;from_base64 ------------- \\x41
hex(blob)
Converts blob to VARCHAR using hexadecimal encoding. Alias: to_hex.
SELECT hex('\xAABB'::BLOB) AS hex;hex ------ AABB
md5(blob)
Returns the MD5 hash of the blob as a VARCHAR.
SELECT md5('\xAABB'::BLOB) AS md5;md5 ---------------------------------- 58cea1f6b2b06520613e09af90dc1c47
md5_number(blob)
Returns the MD5 hash of the blob as a HUGEINT.
SELECT md5_number('\xAABB'::BLOB) AS md5_number;md5_number ---------------------------------------- 94525045605907259200829535064523132504
octet_length(blob)
Number of bytes in blob.
SELECT octet_length('\xAABB'::BLOB) AS octet_length; octet_length
--------------
2overlay(blob PLACING replacement FROM start [FOR count])
Replaces count bytes of blob, starting at byte start (counting from 1), with replacement. Without count, it replaces as many bytes as replacement has; a count of 0 inserts replacement. A start below 1 fails with negative substring length not allowed.
SELECT overlay('\xAABBCCDD'::BLOB PLACING '\xFF'::BLOB FROM 2) AS overlay, overlay('\xAABBCCDD'::BLOB PLACING '\xFF'::BLOB FROM 2 FOR 0) AS inserted;overlay | inserted -------------+--------------- \\xaaffccdd | \\xaaffbbccdd
position(search IN blob)
Returns the byte position of the first occurrence of search in blob, counting from 1. Returns 0 if no match is found.
SELECT position('\xBB'::BLOB IN '\xAABB'::BLOB) AS position; position
----------
2read_blob(source)
Returns the content from source (a filename, a list of filenames, or a glob pattern) as a BLOB. See the read_blob guide for more details.
SELECT content FROM read_blob('hello.bin');content ---------------- \x68656c6c6f0a
repeat(blob, count)
Repeats the blob count number of times.
SELECT repeat('\xAABB'::BLOB, 5) AS repeat;repeat ------------------------- \\xaabbaabbaabbaabbaabb
sha1(blob)
Returns a VARCHAR with the SHA-1 hash of the blob.
SELECT sha1('\xAABB'::BLOB) AS sha1;sha1 ------------------------------------------ 65b1e351a6cbfeb41c927222bc9ef53aad3396b0
sha256(blob)
Returns a VARCHAR with the SHA-256 hash of the blob.
SELECT sha256('\xAABB'::BLOB) AS sha256;sha256 ------------------------------------------------------------------ d798d1fac6bd4bb1c11f50312760351013379a0ab6f0a8c0af8a506b96b2525a
substring(blob, start[, length])
Extracts length bytes of blob starting at byte start, counting from 1, or every byte from start to the end when length is omitted. Also written substring(blob FROM start [FOR length]); substr is an alias. Positions before the first byte count toward length, so substring(blob FROM 0 FOR 2) returns one byte, and a start past the end returns an empty BLOB. A negative length fails with negative substring length not allowed.
SELECT substring('\xAABBCCDD'::BLOB FROM 2 FOR 2) AS substring, substr('\xAABBCCDD'::BLOB, 3) AS substr, substring('\xAABBCCDD'::BLOB FROM 0 FOR 2) AS from_zero;substring | substr | from_zero -----------+---------+----------- \\xbbcc | \\xccdd | \\xaa
to_base64(blob)
Converts a blob to a base64 encoded string. Alias: base64.
SELECT to_base64('A'::BLOB) AS to_base64;to_base64 ----------- QQ==
unbin(value)
Converts a value from binary representation to a blob. Alias: from_binary.
SELECT unbin('0110') AS unbin;unbin ------- \\x06
unhex(value)
Converts a value from hexadecimal representation to a blob. Alias: from_hex.
SELECT unhex('2A') AS unhex;unhex ------- \\x2a