Skip to main content


Removes padding from the ends of the given string.


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


  • 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




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  |

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.

For more information, refer to Implicit Collation in Special Cases.