# SUBSTRING

> For the complete documentation index, see [llms.txt](https://docs.redpanda.com/llms.txt). Component-specific: [cloud-data-platform-full.txt](https://docs.redpanda.com/cloud-data-platform-full.txt)

---
title: SUBSTRING
latest-operator-version: v26.1.4
latest-console-tag: v3.7.3
latest-connect-version: 4.93.0
latest-redpanda-tag: v26.1.9
docname: sql/sql-functions/string-functions/substring
page-component-name: cloud-data-platform
page-version: master
page-component-version: master
page-component-title: Cloud
page-relative-src-path: sql/sql-functions/string-functions/substring.adoc
page-edit-url: https://github.com/redpanda-data/cloud-docs/edit/main/modules/reference/pages/sql/sql-functions/string-functions/substring.adoc
description: The `SUBSTRING` function extracts a part of a string. `SUBSTR` is an alias.
page-topic-type: reference
page-git-created-date: "2026-05-26"
page-git-modified-date: "2026-05-26"
---

<!-- Source: https://docs.redpanda.com/cloud-data-platform/reference/sql/sql-functions/string-functions/substring.md -->

The `SUBSTRING` function extracts a part of a string and returns the result. `SUBSTR` is an alias for `SUBSTRING`. Both names produce the same result.

## [](#syntax)Syntax

Use either name (`SUBSTRING` or `SUBSTR`) with the same syntax:

**2 Arguments**

```sql
SUBSTRING(string, start_position)
```

**3 Arguments**

```sql
SUBSTRING(string, start_position, length)
```

> 💡 **TIP**
>
> Both syntaxes have input and return of type `string`.

### [](#start-position)Start position

Use `start_position` as the starting position, specifying the part from where the substring is to be returned. It is written as an integer value.

| Input | Return |
| --- | --- |
| start_position < string | The start_position is a given character in the string. The count starts from the first character. |
| start_position > string | Returns an empty substring. |
| start_position = negative value | The count starts from the provided negative value, with subsequent characters yielded as it approaches 0. |

If the index is less than or equal to 0, no characters are returned. Once it exceeds 0, characters from the string are yielded, starting from the first one.

### [](#length)Length

Use `length` to determine the number of characters to be extracted. It can be one or more characters.

| Input | Return |
| --- | --- |
| length = 0 | Returns an empty substring. |
| length is not set | The function starts from the specified start_position and ends at the last character of the string. |
| length = negative value | Returns an error. |

## [](#examples)Examples

### [](#extract-a-substring-from-position-1)Extract a substring from position 1

```sql
SELECT SUBSTRING('RedpandaDocumentation', 1, 8);
```

This displays the substring from position 1:

```sql
substring
-----------
 Redpanda
```

### [](#substring-with-specified-start_position-and-length)`SUBSTRING` with specified `start_position` and `length`

In this example, the `start_position` is set to position 6 and 5 characters are extracted:

```sql
SELECT SUBSTRING('Watermelon', 6, 5) AS "Fruit";
```

The query returns:

```sql
Fruit
-------
 melon
```

### [](#substring-with-length-0)`SUBSTRING` with `length` = 0

This query extracts a string with `length` = 0:

```sql
SELECT SUBSTRING('Watermelon', 6, 0) AS "Fruit";
```

This displays an empty output as there is no `length` specified:

```sql
Fruit
-------
```

### [](#substring-with-length-negative-value)`SUBSTRING` with `length` = negative value

This example shows what happens when `length` is specified with a negative value:

```sql
SELECT SUBSTRING('Watermelon', 6, -2) AS "Fruit";
```

Instead of extracting the string from the last characters, it returns an error:

```sql
ERROR:  Length of substring cannot be negative
```

### [](#substring-with-start_position-string)`SUBSTRING` with `start_position` > `string`

The string `Watermelon` has only ten characters. This example shows what happens when the specified `start_position` is larger than the string’s characters:

```sql
SELECT SUBSTRING('Watermelon', 20, 2) AS "Fruit";
```

This displays an empty output:

```sql
Fruit
-------
```

### [](#substring-with-2-arguments)`SUBSTRING` with 2 arguments

In this example, the `start_position` is set to position 6:

```sql
SELECT SUBSTRING('database', 6) AS "Result";
```

This displays the substring from position 6:

```sql
Result
--------
 ase
```