Use GETRANGE to read part of the string stored at a key.
<start> and <end> are zero-based byte offsets, both inclusive, and may be negative to count from the end, so GETRANGE key 0 -1 returns the whole value. Offsets outside the string are clamped rather than treated as errors, and a missing key returns an empty string.
Since the range is applied on the server, this is how you read the head of a large value, or a fixed-width field inside one, without transferring the rest. SUBSTR is accepted as a deprecated alias with the same arguments and reply.
Syntax#
Arguments#
| Argument | Required | Repeatable | Description |
|---|---|---|---|
<key> | Yes | No | Redis key targeted by the command. |
<start> | Yes | No | Start byte offset; negative values count from the end. |
<end> | Yes | No | End byte offset, inclusive; negative values count from the end. |
Important points#
SUBSTRis accepted as a deprecated alias with the same arguments and reply. New code should useGETRANGE.
Response#
The reply reports the result of the operation. Error replies have the same shape in RESP2 and RESP3 and are surfaced as exceptions by the SDKs below.
| Protocol | Reply |
|---|---|
| RESP2 | Bulk string |
| RESP3 | Bulk string |
Client libraries often decode bulk strings, maps, sets, and numeric strings into language-native values. The table describes the Redis wire reply.
Examples#
TCP examples use the TLS REDIS_URL from the Upstash console. REST examples use UPSTASH_REDIS_REST_URL and UPSTASH_REDIS_REST_TOKEN.