SQL TRIM

The SQL TRIM() function is used to remove the leading or trailing spaces of a string. Different databases have different writtings:

SQL Server LTRIM(), RTRIM()
Oracle LTRIM(), RTRIM()
MySQL TRIM(), LTRIM(), RTRIM()

In SQL Server or Oracle it doesn’t have Trim() function, we can combine LTrim() and RTrim():

RTRIM(LTRIM(str))

SQL TRIM Syntax

Remove both leading or trailing spaces:

TRIM (str)
Remove only leading spaces (left trim):

LTRIM (str)
Remove only trailing spaces (right trim):

RTRIM (str)

SQL TRIM Example

SELECT TRIM ('   test ')

The result will look like:

'test'

Note: It will not have the single quote in the result.

SELECT LTRIM ('   test ')

The result will look like:

'test '
SELECT RTRIM ('   test ')

The result will look like:

'   test'
SELECT LTRIM(RTRIM ('   test '))

The result will look like:

'test'

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>