Upstash Documentation

GETRANGE

Get substring of string.
2 min read

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#

ArgumentRequiredRepeatableDescription
<key>YesNoRedis key targeted by the command.
<start>YesNoStart byte offset; negative values count from the end.
<end>YesNoEnd byte offset, inclusive; negative values count from the end.

Important points#

  • SUBSTR is accepted as a deprecated alias with the same arguments and reply. New code should use GETRANGE.

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.

ProtocolReply
RESP2Bulk string
RESP3Bulk string
Note

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.

Redis CLI
@upstash/redis
upstash_redis
ioredis
node-redis
redis-py
go-redis
jedis
redis-rs