TRIM

Removes padding from the ends of the given string.

Syntax

TRIM ([[BOTH | LEADING | TRAILING] [padding] FROM] str)

Arguments

  • padding: any string or binary object (optional, defaults to " ")

  • str: any string or binary object

If no keyword is specified, defaults to BOTH.

Return Type

String

Examples

Note

This function removes only space " " characters. Other whitespace characters like tab \t, newline \n, etc are preserved.

SELECT TRIM(' ohai ') AS t,
TRIM(LEADING FROM ' ohai ') AS l,
TRIM(TRAILING FROM ' ohai ') AS r;
+------+-----------+----------+
| t    | l         | r        |
+------+-----------+----------+
| ohai | ohai      |     ohai |
+------+-----------+----------+
SELECT TRIM("abra" FROM "abracadabra") AS t;
+------+
| t    |
+------+
| cad  |
+------+

Caution

Implicit Collation

When character_set_server is set to utf8, string literals with characters using 4-byte encoding are implicitly assigned binary collation and processed as a sequence of bytes rather than characters. This implicit conversion to binary collation causes string functions to return unexpected results. To avoid using implicit binary collation, either use explicit type casting or use database columns defined with the utf8mb4 character set.

Last modified: April 4, 2023

Was this article helpful?