# GETRANGE

> Return a substring of the string stored at a key.

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

```
GETRANGE key start end
```

- Group: String
- Complexity: O(N) where N is the length of the returned substring
- 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

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

## Examples

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

## Compatibility

Same as Redis 7 and Valkey 8.

## See also

[SETRANGE](https://docs.hotkv.com/commands/setrange/index.md), [SUBSTR](https://docs.hotkv.com/commands/substr/index.md),
[STRLEN](https://docs.hotkv.com/commands/strlen/index.md), [GET](https://docs.hotkv.com/commands/get/index.md)