# SETRANGE

> Overwrite part of a string at a byte offset.

URL: https://docs.hotkv.com/commands/setrange/

```
SETRANGE key offset value
```

- Group: String
- Complexity: O(1), plus the time to grow the string if needed
- 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

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

## Examples

```
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"
```

## Compatibility

Same as Redis 7 and Valkey 8.

## See also

[GETRANGE](https://docs.hotkv.com/commands/getrange/index.md), [APPEND](https://docs.hotkv.com/commands/append/index.md),
[SETBIT](https://docs.hotkv.com/commands/setbit/index.md)