Skip to content

Ask the HotKV docs

Answers come only from these docs and can be wrong; check the linked pages before you rely on them.

GETRANGE

Last updated
GETRANGE key start end
Group
String
Complexity
O(N) where N is the length of the returned substring
Arity
4 (exactly, counting the command name)
Flags
read-only
Keys
first 1, last 1, step 1
HotKV Cloud
All plans
On-Premise
no license needed

GETRANGE returns the bytes of the string at key from offset start to offset end, both included. Offsets start at 0. Negative offsets count back from the end of the string, so -1 is the last byte. Offsets past either end are clamped to the string, and a range that ends before it starts returns an empty string. A missing key reads as an empty string.

Case RESP2 RESP3
Always Bulk string: the substring (possibly empty) Bulk string

Errors:

  • ERR value is not an integer or out of range when start or end is not an integer.
  • WRONGTYPE when the key holds a value that is not a string.
127.0.0.1:6379> SET phrase "hotkv rocks"
OK
127.0.0.1:6379> GETRANGE phrase 0 4
"hotkv"
127.0.0.1:6379> GETRANGE phrase -5 -1
"rocks"
127.0.0.1:6379> GETRANGE phrase 6 100
"rocks"
127.0.0.1:6379> GETRANGE phrase 5 2
""

Same as Redis 7 and Valkey 8.

SETRANGE, SUBSTR, STRLEN, GET