trim
Removes consecutive spaces or specified characters from the beginning and end of the str argument. Removing specified characters are supported from StarRocks 2.5.0.
Syntax
VARCHAR trim(VARCHAR str[, VARCHAR characters])
Parameters
str: required, the string to trim, which must evaluate to a VARCHAR value.
characters: optional, the characters to remove, which must be a VARCHAR value. If this parameter is not specified, spaces are removed from the string by default. If this parameter is set to an empty string, an error is returned.
Return value
Returns a VARCHAR value.
Examples
Example 1: Remove the five spaces from the beginning and end of the string.
MySQL > SELECT trim(" ab c ");
+-------------------+
| trim(' ab c ') |
+-------------------+
| ab c |
+-------------------+
1 row in set (0.00 sec)
Example 2: Remove specified characters from the beginning and end of the string.
MySQL > SELECT trim("abcd", "ad");
+--------------------+
| trim('abcd', 'ad') |
+--------------------+
| bc |
+--------------------+
MySQL > SELECT trim("xxabcdxx", "x");
+-----------------------+
| trim('xxabcdxx', 'x') |
+-----------------------+
| abcd |
+-----------------------+
MySQL-compatible TRIM(... FROM ...) syntax
In addition to trim(str) and trim(str, characters), StarRocks supports the MySQL/SQL-standard form:
TRIM([{BOTH | LEADING | TRAILING}] [remstr] FROM str)
BOTH(assumed when the specifier is omitted) trims both ends;LEADINGtrims only the start;TRAILINGtrims only the end.remstris an optional string literal. If omitted, spaces are trimmed.remstris removed as a whole substring, repeatedly — it is NOT treated as a set of characters. This differs fromtrim(str, characters), where the second argument is a character set.
MySQL > SELECT TRIM(LEADING 'x' FROM 'xxxbarxxx');
+------------------------------------+
| TRIM(LEADING 'x' FROM 'xxxbarxxx') |
+------------------------------------+
| barxxx |
+------------------------------------+
MySQL > SELECT TRIM(TRAILING 'xyz' FROM 'barxxyzxyz');
+----------------------------------------+
| TRIM(TRAILING 'xyz' FROM 'barxxyzxyz') |
+----------------------------------------+
| barx |
+----------------------------------------+
MySQL > SELECT TRIM(BOTH 'ab' FROM 'abfooab');
+--------------------------------+
| TRIM(BOTH 'ab' FROM 'abfooab') |
+--------------------------------+
| foo |
+--------------------------------+
Note the difference between the substring (FROM) form and the character-set (comma) form:
-- substring: removes the whole 'xyz' once, leaving 'barx'
MySQL > SELECT TRIM(TRAILING 'xyz' FROM 'barxxyz'); -- 'barx'
-- character set: removes any trailing x/y/z, leaving 'bar'
MySQL > SELECT rtrim('barxxyz', 'xyz'); -- 'bar'
The FROM form is backed by the builtins trim_string(str, remstr), ltrim_string(str, remstr), and rtrim_string(str, remstr), which can also be called directly. remstr must be a constant string literal.
NOTE
remstrmust not be NULL. Passing a NULLremstrto these builtins (for exampletrim_string('abc', CAST(NULL AS STRING))) returns an error rather than NULL. This is consistent with the comma formtrim(str, characters)and differs from MySQL, which returns NULL. A NULLstrstill propagates to NULL (for exampleTRIM('x' FROM NULL)returns NULL).