Mysql Trim Function

MySQL: TRIM Function

This tutorial explains how to use the MySQL TRIM function with syntax and examples.

Description

The MySQL TRIM function removes all specified characters either from the beginning or the end of a string.

Syntax

The syntax for the TRIM function in MySQL is:

TRIM( [ LEADING | TRAILING | BOTH ] [ trim_character FROM ] string )

Parameters or Arguments

LEADING

Optional. Removes the trim_character from the front of string.

TRAILING

Optional. Removes the trim_character from the end of string.

BOTH

Optional. Removes the trim_character from the front and end of string.

trim_character

Optional. The character that will be removed from string. If this parameter is omitted, it will remove space characters from string.

string

The string to trim.

Note

  • If you do not specify a value for the first parameter (LEADING, TRAILING, BOTH), the TRIM function will default to BOTH and remove trim_character from both the front and end of string.
  • If you do not specify a trim_character, the TRIM function will default the character to be removed as a space character.

Applies To

The TRIM function can be used in the following versions of MySQL:

  • MySQL 5.7, MySQL 5.6, MySQL 5.5, MySQL 5.1, MySQL 5.0, MySQL 4.1, MySQL 4.0, MySQL 3.23

Example

Let's look at some MySQL TRIM function examples and explore how to use the TRIM function in MySQL.

For example:

mysql> SELECT TRIM(LEADING ' ' FROM '  AODBA.com  ');
Output: 'AODBA.com  '

mysql> SELECT TRIM(TRAILING ' ' FROM '  AODBA.com  ');
Output: '  AODBA.com'

mysql> SELECT TRIM(BOTH ' ' FROM '  AODBA.com  ');
Output: 'AODBA.com'

mysql> SELECT TRIM(' ' FROM '  AODBA.com  ');
Output: 'AODBA.com'

mysql> SELECT TRIM('   AODBA.com   ');
Output: 'AODBA.com'

mysql> SELECT TRIM(LEADING '0' FROM '000123');
Output: '123'