GETRANGE
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.
Return value
Section titled “Return value”| Case | RESP2 | RESP3 |
|---|---|---|
| Always | Bulk string: the substring (possibly empty) | Bulk string |
Errors:
ERR value is not an integer or out of rangewhenstartorendis not an integer.WRONGTYPEwhen the key holds a value that is not a string.
Examples
Section titled “Examples”127.0.0.1:6379> SET phrase "hotkv rocks"OK127.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""Compatibility
Section titled “Compatibility”Same as Redis 7 and Valkey 8.
See also
Section titled “See also”Was this page helpful?
Thanks for the feedback.