SQL TRIM() Function

The SQL TRIM() function is used to removes all whitespaces (by default) or any specified character(by user defined) from a string.

The SQL TRIM() function removes characters either from the starting or the ending or both side of a given input string.

Note: The SQL TRIM() function will not remove any character in between the string character(s), also if it is found.

The SQL TRIM() function is supports or work with only character based table columns or field.

It can be used in any valid SQL SELECT statement as well in SQL where clause.

Search Keys

  • sql trim function
  • trim sql
  • rtrim sql
  • sql server rtrim
  • sql ltrim
  • t sql trim
  • sql trim string
  • sql server ltrim
  • trim command
  • trim function
  • sql trim whitespace
  • sql left trim
  • left trim sql
  • ltrim rtrim
  • trim function sql
  • sql right trim

SQL TRIM() Syntax

The below syntax is used to remove only whitespaces from both(start and end) side from a given input string or text.


SELECT TRIM(string or text);

The below syntax is used to remove all specified characters by user either from starting(leading) side or ending(trailing) side or both side from a given input string or text.


SELECT TRIM( [LEADING | TRAILING | BOTH] [trim_character FROM] string1 );

Parameter Name Description
LEADING OPTIONAL. Removes the "trim_character" from the front or left side of string.
TRAILING OPTIONAL. Removes the "trim_character" from the end or right side of string.
BOTH OPTIONAL. Removes the "trim_character" from the both(left and right) side of string.
trim_character OPTIONAL. The character that will be removed from the given input string. If this argument is omitted, it will remove whitespace characters from string.
string1 Required. The input string to trim by TRIM() function.

SQL TRIM() Example - Using Expression Or Formula

The following SQL SELECT statement concatenate three strings as a single string.

SELECT TRIM('   Simmanchith.com   ') AS 'Trimmed String';

The result of above query is:

Trimmed String

SQL TRIM() Function More Example

Input Value Result
TRIM('     SQL Tutorial') SQL Tutorial
TRIM('SQL Database Tutorial     ') SQL Database Tutorial
TRIM('     Welcome To Simmanchith.com     ') Welcome To Simmanchith.com
TRIM('     Database    Programming    Tutorial     ') Database    Programming    Tutorial
TRIM(LEADING ' ' FROM '     DB      Programming     ') DB      Programming     
TRIM(TRAILING ' ' FROM '     DB      Tutorial     ')      DB      Tutorial
TRIM(BOTH ' ' FROM '     DB      Programming      Tutorial     ') DB      Programming      Tutorial
TRIM(LEADING '123' FROM '123Simmanchith123.com123') Simmanchith123.com123
TRIM(TRAILING '123' FROM '123Simmanchith123.com123') 123Simmanchith123.com
TRIM(BOTH '123' FROM '123Simmanchith123.com123') Simmanchith123.com

Note: The TRIM() function will not remove any characters (whitespaces or user defined) in between the string.

Search Keys

  • ms sql server trim
  • sql trim characters
  • ltrim rtrim sql server
  • db2 trim spaces
  • sql server trim characters
  • sql trim example
  • sql ltrim example
  • sql trim command
  • sql server trim leading spaces
  • trim spaces sql
  • oracle trim trailing spaces
  • sql trim leading zeros
  • db2 trim whitespace
  • sql server remove spaces
  • sql server space function
  • string functions in sql