> For the complete documentation index, see [llms.txt](https://cortex-docs.paloaltonetworks.com/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://cortex-docs.paloaltonetworks.com/xql-command-reference-guide/readme/functions/trim.md).

# trim

Use the `trim()` function to remove specified characters or whitespace from the beginning and end of a given string.

## Syntax

```sql
trim (<string>, [trim_characters])
```

## Parameters

| Name              | Type   | Required | Description                                                                                                                                            |
| ----------------- | ------ | -------- | ------------------------------------------------------------------------------------------------------------------------------------------------------ |
| `string`          | string | Yes      | The input string field or literal value to be trimmed.                                                                                                 |
| `trim_characters` | string | No       | A string containing the characters you want to remove. If this parameter is omitted, the function will remove whitespace characters (spaces and tabs). |

## Returns

The `trim()` function returns a string with the specified characters or whitespace removed from both the start and end.

## Usage notes

* The function removes characters from both the beginning (left) and end (right) of the string.
* If `trim_characters` is not provided, the function defaults to removing leading and trailing spaces and tabs.
* The function is versatile for cleaning and standardizing string data within queries.

## Examples

### Example 1: Removing leading and trailing whitespace (default behavior)

**Goal**: Remove spaces from both ends of a literal string where no specific characters are defined.

**XQL code**:

```sql
config timeframe = 1d 
| dataset = sample_xql_raw 
| alter original_string = "   User login successful   " 
| alter trimmed_string = trim(original_string) 
| fields event_id, original_string, trimmed_string 
| limit 1 
```

**Explanation**: The query detects and removes the leading and trailing spaces from the `original_string`, resulting in a clean string "User login successful".

**Output**:

| EVENT\_ID | ORIGINAL\_STRING          | TRIMMED\_STRING         |
| --------- | ------------------------- | ----------------------- |
| 101       | " User login successful " | "User login successful" |

### Example 2: Removing specific characters from both ends

**Goal**: Remove explicit characters ("w") from both ends of the `dst_domain` string.

**XQL code**:

```sql
config timeframe = 1d 
| dataset = sample_xql_raw 
| filter event_id = 103 
| alter domain_without_w = trim(dst_domain, "w")
| fields event_id, dst_domain, domain_without_w 
```

**Explanation**: The function removes all leading and trailing 'w' characters from the `dst_domain` string (for example, "[www.google.com](http://www.google.com)" becomes ".google.com").

**Output**:

| EVENT\_ID | DST\_DOMAIN                               | DOMAIN\_WITHOUT\_W |
| --------- | ----------------------------------------- | ------------------ |
| 103       | "[www.google.com](http://www.google.com)" | ".google.com"      |

### Example 3: Removing multiple specific characters

**Goal**: Remove a set of explicit characters ("w", "m", and ".") from both ends of the `dst_domain` string.

**XQL code**:

```sql
config timeframe = 1d 
| dataset = sample_xql_raw 
| filter event_id = 110 
| alter domain_without_w_m_period = trim(dst_domain, "wm.")
| fields event_id, dst_domain, domain_without_w_m_period
```

**Explanation**: The function removes all occurrences of 'w', 'm', and '.' characters found at the start or end of the string. For "[www.mongodb.com](http://www.mongodb.com)", the leading "[www](http://www)." and the trailing "m" are removed, leaving "ongodb.co".

**Output**:

| EVENT\_ID | DST\_DOMAIN                                 | DOMAIN\_WITHOUT\_W\_M\_PERIOD |
| --------- | ------------------------------------------- | ----------------------------- |
| 110       | "[www.mongodb.com](http://www.mongodb.com)" | "ongodb.co"                   |

## Related articles

* **Stages**: [`alter`](/xql-command-reference-guide/readme/stages/alter.md), [`filter`](/xql-command-reference-guide/readme/stages/filter.md)
* **Functions**: [`ltrim`](/xql-command-reference-guide/readme/functions/ltrim.md), [`rtrim`](/xql-command-reference-guide/readme/functions/rtrim.md)
* **Datasets**: [`xdr_data`](https://www.google.com/search?q=%5Bhttps://docs-cortex.paloaltonetworks.com/r/Cortex-XQL-Schema-Reference-Guide/Introduction%5D\(https://docs-cortex.paloaltonetworks.com/r/Cortex-XQL-Schema-Reference-Guide/Introduction\))


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter, and the optional `goal` query parameter:

```
GET https://cortex-docs.paloaltonetworks.com/xql-command-reference-guide/readme/functions/trim.md?ask=<question>&goal=<endgoal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is optional and describes the broader end goal you are ultimately trying to accomplish on behalf of the user. GitBook uses it to tailor the answer towards what is most useful for that goal.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
