MySQL: TRIM Function

  • Post author:
  • Post category:MySQL
  • Post comments:1 Comment
MySQL TRIM Function

In this guide, we will explain 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 (LEADINGTRAILINGBOTH), 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 TRIM function examples and explore how to use the TRIM function.

For example:

mysql> SELECT TRIM(LEADING ' ' FROM '  adglob.in  ');
Result: 'adglob.in  '

mysql> SELECT TRIM(TRAILING ' ' FROM '  adglob.in  ');
Result: '  adglob.in'

mysql> SELECT TRIM(BOTH ' ' FROM '  adglob.in  ');
Result: 'adglob.in'

mysql> SELECT TRIM(' ' FROM '  adglob.in  ');
Result: 'adglob.in'

mysql> SELECT TRIM('   adglob.in   ');
Result: 'adglob.in'

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

mysql> SELECT TRIM(TRAILING '1' FROM 'Adglob1');
Result: 'Adglob'

mysql> SELECT TRIM(BOTH '123' FROM '123Adglob123');
Result: 'Adglob'

Next Topic : Click Here

This Post Has One Comment

Leave a Reply