SETRANGE
SETRANGE key offset value
- Group
- String
- Complexity
- O(1), plus the time to grow the string if needed
- Arity
- 4 (exactly, counting the command name)
- Flags
- write
- Keys
- first 1, last 1, step 1
- HotKV Cloud
- All plans
- On-Premise
- no license needed
SETRANGE writes value into the string at key, starting at byte offset.
If the string is shorter than offset, it is padded with zero bytes first. A
missing key is treated as an empty string, so the key is created. An empty
value changes nothing (and does not create the key). The key keeps its time
to live.
Return value
Section titled “Return value”| Case | RESP2 | RESP3 |
|---|---|---|
| Always | Integer: the length of the string after the write | Integer |
Errors:
ERR offset is out of rangewhenoffsetis negative.ERR value is not an integer or out of rangewhenoffsetis not an integer.ERR string exceeds maximum allowed size (proto-max-bulk-len)when the result would be longer thanproto-max-bulk-len(512 MB by default).WRONGTYPEwhen the key holds a value that is not a string.
Examples
Section titled “Examples”127.0.0.1:6379> SET motto "hello world"OK127.0.0.1:6379> SETRANGE motto 6 "hotkv"(integer) 11127.0.0.1:6379> GET motto"hello hotkv"127.0.0.1:6379> SETRANGE padded 3 "x"(integer) 4127.0.0.1:6379> GET padded"\x00\x00\x00x"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.