Mariadb Trim Function

MariaDB: TRIM Function

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

Description

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

Syntax

The syntax for the TRIM function in MariaDB 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 MariaDB:

  • MariaDB 10

Example

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

For example:

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

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

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

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

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

SELECT TRIM(LEADING 'a' FROM 'aaa987');
Output: '987'

SELECT TRIM(TRAILING '1' FROM 'Tech11');
Output: 'Tech'

SELECT TRIM(BOTH '123' FROM '123Tech123');
Output: 'Tech'