> ## Documentation Index
> Fetch the complete documentation index at: /documents/llms.txt
> Use this file to discover all available pages before exploring further.

# sub_bitmap

> Intercepts len elements from a BITMAP value src starting from the position specified by offset.

Intercepts `len` elements from a BITMAP value `src` starting from the position specified by `offset`. The output elements are a subset of `src`.

This function is mainly used for scenarios such as paginated queries. It is supported from v2.5.

This function is similar to [bitmap\_subset\_limit](/documents/4.2/sql-reference/sql-functions/bitmap-functions/bitmap_subset_limit.md). The difference is that this function intercepts elements starting from an offset whereas bitmap\_subset\_limit intercepts elements starting from an element value (`start_range`).

## Syntax

```Haskell theme={null}
BITMAP sub_bitmap(BITMAP src, BIGINT offset, BIGINT len)
```

## Parameters

* `src`: the BITMAP value from which you want to obtain elements.
* `offset`: the starting position. It must be a BIGINT value. Note the following points when you use `offset`:
  * Offsets start from 0.
  * Negative offsets are counted from right to left. See Examples 3 and 4.
  * If the starting position specified by `offset` exceeds the actual length of the BITMAP value, NULL is returned. See Example 6.
* `len`: the number of elements to obtain. It must be a BIGINT value greater than or equal to 1. If the number of matching elements is less than the value of `len`, all the matching elements are returned. See Examples 2, 3, and 7.

## Return value

Returns a value of the BITMAP type. NULL is returned if any of the input parameters is invalid.

## Examples

In the following examples, the input of sub\_bitmap() is the output of [bitmap\_from\_string](/documents/4.2/sql-reference/sql-functions/bitmap-functions/bitmap_from_string.md). For example, `bitmap_from_string('1,1,3,1,5,3,5,7,7,9')` returns `1, 3, 5, 7, 9`. sub\_bitmap() takes this BITMAP value as the input.

Example 1: Obtain two elements from the BITMAP value with the offset set to 0.

```Plaintext theme={null}
select bitmap_to_string(sub_bitmap(bitmap_from_string('1,1,3,1,5,3,5,7,7,9'), 0, 2)) value;
+-------+
| value |
+-------+
| 1,3   |
+-------+
```

Example 2: Obtain 100 elements from the BITMAP value with the offset set to 0. 100 exceeds the length of the BITMAP value and all the matching elements are returned.

```Plaintext theme={null}
select bitmap_to_string(sub_bitmap(bitmap_from_string('1,1,3,1,5,3,5,7,7,9'), 0, 100)) value;
+-----------+
| value     |
+-----------+
| 1,3,5,7,9 |
+-----------+
```

Example 3: Obtain 100 elements from the BITMAP value with the offset set to -3. 100 exceeds the length of the BITMAP value and all the matching elements are returned.

```Plaintext theme={null}
select bitmap_to_string(sub_bitmap(bitmap_from_string('1,1,3,1,5,3,5,7,7,9'), -3, 100)) value;
+-------+
| value |
+-------+
| 5,7,9 |
+-------+
```

Example 4: Obtain two elements from the BITMAP value with the offset set to -3.

```Plaintext theme={null}
select bitmap_to_string(sub_bitmap(bitmap_from_string('1,1,3,1,5,3,5,7,7,9'), -3, 2)) value;
+-------+
| value |
+-------+
| 5,7   |
+-------+
```

Example 5: NULL is returned because `-10` is an invalid input of `len`.

```Plaintext theme={null}
select bitmap_to_string(sub_bitmap(bitmap_from_string('1,1,3,1,5,3,5,7,7,9'), 0, -10)) value;
+-------+
| value |
+-------+
| NULL  |
+-------+
```

Example 6: The starting position specified by offset 5 exceeds the length of the BITMAP value `1,3,5,7,9`. NULL is returned.

```Plain theme={null}
select bitmap_to_string(sub_bitmap(bitmap_from_string('1,1,3,1,5,3,5,7,7,9'), 5, 1)) value;
+-------+
| value |
+-------+
| NULL  |
+-------+
```

Example 7: `len` is set to 5 but only two elements match the condition. All of these two elements are returned.

```Plain theme={null}
select bitmap_to_string(sub_bitmap(bitmap_from_string('1,1,3,1,5,3,5,7,7,9'), -2, 5)) value;
+-------+
| value |
+-------+
| 7,9   |
+-------+
```


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.