# trim

> 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: trim
latest-operator-version: v26.2.4
latest-console-tag: v3.12.0
latest-connect-version: 4.113.1
latest-redpanda-tag: v26.2.4
docname: sql/sql-functions/string-functions/trim
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/trim.adoc
page-edit-url: https://github.com/redpanda-data/cloud-docs/edit/main/modules/reference/pages/sql/sql-functions/string-functions/trim.adoc
description: The `trim()` function removes the longest string of characters matching a set of characters from one or both ends of a string.
page-topic-type: reference
page-git-created-date: "2026-09-08"
page-git-modified-date: "2026-09-08"
---

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

The `trim()` function removes the longest string containing only characters from a specified set from the start, end, or both ends of a string. If you don’t specify a set of characters, `trim()` removes spaces. By default, `trim()` removes characters from both ends of the string, the same as [`btrim()`](https://docs.redpanda.com/cloud-data-platform/reference/sql/sql-functions/string-functions/btrim/).

Redpanda SQL also supports the SQL-standard `TRIM(…​ FROM …​)` syntax as an alternative to calling `trim()` directly.

## [](#syntax)Syntax

The syntax of the `trim()` function is:

```sql
TRIM(string)
TRIM(string, characters)
```

Redpanda SQL also accepts the SQL-standard form:

```sql
TRIM([LEADING | TRAILING | BOTH] [characters] FROM string)
```

The `trim()` function and the `TRIM(…​ FROM …​)` syntax accept the following arguments:

-   `string`: The string to trim.

-   `characters`: Optional. The set of characters to remove. Defaults to a space character.

-   `LEADING`, `TRAILING`, `BOTH`: Optional, and only available with the `FROM` syntax. Selects which end of `string` to trim from. Defaults to `BOTH` when omitted.


## [](#examples)Examples

### [](#trim-spaces-from-both-ends)Trim spaces from both ends

This example removes leading and trailing spaces from a string:

```sql
SELECT TRIM('   Redpanda   ');
```

```sql
+-----------+
| btrim     |
+-----------+
| Redpanda  |
+-----------+
```

### [](#trim-a-specified-set-of-characters-using-the-from-syntax)Trim a specified set of characters using the FROM syntax

This example uses the `LEADING …​ FROM` syntax to remove leading `x` or `y` characters from a string that has matching characters on both ends. Only the leading characters are removed; the trailing ones are left in place:

```sql
SELECT TRIM(LEADING 'xy' FROM 'xyxyRedpandaxyxy');
```

```sql
+---------------+
| ltrim         |
+---------------+
| Redpandaxyxy  |
+---------------+
```

### [](#trim-without-specifying-a-direction)Trim without specifying a direction

When you omit `LEADING`, `TRAILING`, or `BOTH`, `TRIM(…​ FROM …​)` trims from both ends, the same as `btrim()`:

```sql
SELECT TRIM('xy' FROM 'xyRedpandayx');
```

```sql
+-----------+
| btrim     |
+-----------+
| Redpanda  |
+-----------+
```