SQL TRIM() Function

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.



Sql trim function using sql trim function, sql trim whitespace, trim function sql.

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.

For MS ACCESS / MY SQL / ORACLE


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.

For MY SQL / ORACLE


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
Simmanchith.com

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.



Sql server trim function using ms sql server trim, ltrim rtrim , sql server trim characters, trim leading spaces, trim trailing spaces, trim leading zeros, trim whitespace, sql server remove spaces, sql server space function, string functions in sql.