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.

SETRANGE

Last updated
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.

Case RESP2 RESP3
Always Integer: the length of the string after the write Integer

Errors:

  • ERR offset is out of range when offset is negative.
  • ERR value is not an integer or out of range when offset is not an integer.
  • ERR string exceeds maximum allowed size (proto-max-bulk-len) when the result would be longer than proto-max-bulk-len (512 MB by default).
  • WRONGTYPE when the key holds a value that is not a string.
127.0.0.1:6379> SET motto "hello world"
OK
127.0.0.1:6379> SETRANGE motto 6 "hotkv"
(integer) 11
127.0.0.1:6379> GET motto
"hello hotkv"
127.0.0.1:6379> SETRANGE padded 3 "x"
(integer) 4
127.0.0.1:6379> GET padded
"\x00\x00\x00x"

Same as Redis 7 and Valkey 8.

GETRANGE, APPEND, SETBIT