TRIM

TRIM is an SQL function that removes specified prefixes or suffixes from a string. The most common use of TRIM is to remove leading and/or trailing spaces.

TRIM([REMOVING string] FROM string)

  • removing string: This parameter specifies the characters you want to remove from the string. This can include leading and trailing characters, such as spaces or specific characters enclosed within single quotes.
  • from string: This parameter specifies the string from which the REMOVING string characters are to be trimmed. It is the main string on which the TRIM operation is performed.

Example

SELECT TRIM(' HelloWorld ') AS TrimmedString;

Output

+---------------+
| TrimmedString |
+---------------+
| HelloWorld |
+---------------+

Explanation

In the given example, the MySQL TRIM() function is used to remove the leading and trailing spaces from the string ' HelloWorld '. The result is 'HelloWorld'.

TRIM([LEADING | TRAILING | BOTH] [characters TO trim] FROM string)

  • leading: This parameter specifies that the trim function should remove characters from the beginning (left side) of the string.
  • trailing: This parameter is used when it is desired to remove characters from the end (right side) of the string.
  • both: By using this parameter, the trim function is directed to remove characters from both the beginning and end of the string.
  • characters to trim: This is the set of characters that the trim function should remove from the string. If this parameter is not specified, the trim function will remove whitespace characters by default.
  • from string: This parameter designates the input string from which the specific characters should be removed.

Example

SELECT TRIM(' PostgreSQL ');

Output

PostgreSQL

Explanation

The TRIM function in PostgreSQL removes specified leading or trailing characters (spaces are default) from a string. In the provided example, the TRIM function removes the leading and trailing spaces from the string ’ PostgreSQL ’.

TRIM( [ characters FROM ] string )

  • characters: An optional parameter that specifies the set of characters to be removed from the string. If not specified, spaces will be removed.
  • from: A keyword in the TRIM function that signals the start of the string from which the characters should be removed.
  • string: The primary input given to the TRIM function, representing the sequence of characters from which specified characters, or spaces by default, will be removed. This results in the return of a new string without leading or trailing instances of the specified characters.

Example

DECLARE @ExampleString VARCHAR(20) = ' SQL Server ';
SELECT TRIM(@ExampleString) AS TrimmedString;

Output

TrimmedString
---------------
'SQL Server'

Explanation

The TRIM function in SQL Server is used to remove the leading and trailing spaces of a string. In the provided code, the TRIM function is used to trim spaces from the string ' SQL Server '. The output ‘SQL Server’ is the result of the operation, which displays the string without any leading or trailing spaces.

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

  • leading: Removes all specified trim characters from the beginning of a string.
  • trailing: Removes all specified trim characters from the end of a string.
  • both: Removes all specified trim characters from both the beginning and end of a string.
  • trim_character: The character to be removed from the string.
  • string: The source string from which the specified trim character is to be removed.

Example

SELECT TRIM(' Oracle SQL ') FROM dual;

Output

Oracle SQL

Explanation

The TRIM function removes leading and trailing spaces from the string ’ Oracle SQL ’, resulting in the string ‘Oracle SQL’.

TRIM([leading/trailing/both] [characters FROM] string)

  • [leading/trailing/both]: This parameter specifies where characters should be removed. If ‘leading’ is chosen, it will remove characters from the start of the string. If ‘trailing’ is chosen, it will remove characters from the end of the string. When ‘both’ is specified, it will remove characters from both ends of the string.
  • [characters from]: This optional parameter is used to designate what specific characters are to be removed from the string. If this parameter is not provided, the TRIM function will remove whitespace characters by default.
  • string: This is the string of characters where the TRIM function is applied. It may be a text string literal, a variable holding a string, or a column name whose data type is some kind of character string.

Example

SELECT TRIM(' SQLite ');

Output

'SQLite'

Explanation

The TRIM() function in SQL is used to remove the leading and trailing spaces from a string. The above code removes the spaces at the beginning and the end of ’ SQLite ’, resulting in ‘SQLite’.

For in-depth explanations and examples SQL keywords where you write your SQL, install our extension.