The SUB_BINARY function extracts a binary subsequence from a VARBINARY value. You can specify the starting position and the length of bytes to extract. The first byte position in the binary is 1.
sub_binary(<bin>, <pos> [, <len>])
| Parameter | Description |
|---|---|
<bin> | Source binary value. Type: VARBINARY |
<pos> | Starting byte position, can be negative. Type: INT |
<len> | Optional parameter, number of bytes to extract. Type: INT |
Returns VARBINARY type, representing the extracted binary subsequence.
Special cases:
SELECT sub_binary(x'61626331', 2);
+--------------------------------------------------------+ | sub_binary(x'61626331', 2) | +--------------------------------------------------------+ | 0x626331 | +--------------------------------------------------------+
SELECT sub_binary(x'61626331', -2);
+----------------------------------------------------------+ | sub_binary(x'61626331', -2) | +----------------------------------------------------------+ | 0x6331 | +----------------------------------------------------------+
SELECT sub_binary(x'61626331', 0);
+--------------------------------------------------------+ | sub_binary(x'61626331', 0) | +--------------------------------------------------------+ | 0x | +--------------------------------------------------------+
SELECT sub_binary(x'61626331', 5);
+--------------------------------------------------------+ | sub_binary(x'61626331', 5) | +--------------------------------------------------------+ | 0x | +--------------------------------------------------------+
SELECT sub_binary(x'61626331646566', 2, 2);
+--------------------------------------------------------+ | sub_binary(x'61626331646566', 2, 2) | +--------------------------------------------------------+ | 0x6263 | +--------------------------------------------------------+