trim
Use the trim() function to remove specified characters or whitespace from the beginning and end of a given string.
Syntax
trim (<string>, [trim_characters])Parameters
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_charactersis 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:
Explanation: The query detects and removes the leading and trailing spaces from the original_string, resulting in a clean string "User login successful".
Output:
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:
Explanation: The function removes all leading and trailing 'w' characters from the dst_domain string (for example, "www.google.com" becomes ".google.com").
Output:
103
"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:
Explanation: The function removes all occurrences of 'w', 'm', and '.' characters found at the start or end of the string. For "www.mongodb.com", the leading "www." and the trailing "m" are removed, leaving "ongodb.co".
Output:
110
"www.mongodb.com"
"ongodb.co"
Related articles
Datasets:
xdr_data
Last updated
Was this helpful?
