trim
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().
Redpanda SQL also supports the SQL-standard TRIM(… FROM …) syntax as an alternative to calling trim() directly.
Syntax
The syntax of the trim() function is:
TRIM(string)
TRIM(string, characters)
Redpanda SQL also accepts the SQL-standard form:
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 theFROMsyntax. Selects which end ofstringto trim from. Defaults toBOTHwhen omitted.
Examples
Trim spaces from both ends
This example removes leading and trailing spaces from a string:
SELECT TRIM(' Redpanda ');
+-----------+
| btrim |
+-----------+
| Redpanda |
+-----------+
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:
SELECT TRIM(LEADING 'xy' FROM 'xyxyRedpandaxyxy');
+---------------+
| ltrim |
+---------------+
| Redpandaxyxy |
+---------------+