Dofactory.com
Dofactory.com
Earn income with your data and sql skills
Sign up and we'll send you the best freelance opportunities straight to your inbox.
We're building the largest freelancing marketplace for people like you.
By adding your name & email you agree to our terms, privacy and cookie policies.

SQL TRIM Function

TRIM removes leading and trailing spaces from a string.

TRIM can also remove other characters with an optional parameter.

This function can be replaced by combining LTRIM and RTRIM functions.

Example

#

This example removes leading and trailing spaces from the input string.

SELECT TRIM('  SQL  ') AS Trimmed
Result:  1 record
Trimmed
SQL

Syntax

Syntax for the TRIM function.

TRIM ([characters FROM] string)

[characters FROM ] is optional and specifies characters to be removed.

[characters FROM ] is only supported in SQL Server 2017 and newer.

string -- a string or column name to trim.


More Examples

TRIM. Other characters.

Problem: Remove the specified start and end characters (!, #, and space) from the string.
SELECT TRIM('#! ' FROM ' #! #SQL !    ') AS Trimmed

Supported in SQL Server 2017 and up.

Result:  1 record
Trimmed
SQL

You may also like



Last updated on Dec 21, 2023

Earn income with your data and sql skills
Sign up and we'll send you the best freelance opportunities straight to your inbox.
We're building the largest freelancing marketplace for people like you.
By adding your name & email you agree to our terms, privacy and cookie policies.