SQL > SQL String Functions > Trim

The TRIM function in SQL is used to remove specified prefix or suffix from a string. The most common pattern being removed is white spaces. This function is called differently in different databases:

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

The syntax for these trim functions are:

TRIM( [ [LOCATION] [remstr] FROM ] str)

[LOCATION] can be either LEADING, TRAILING, or BOTH. This function gets rid of the [remstr] pattern from either the beginning of the string or the end of the string, or both. If no [remstr] is specified, white spaces are removed.

LTRIM (str)
LTRIM Removes all white spaces from the beginning of the string.

RTRIM (str)
RTRIM emoves all white spaces at the end of the string.

Example 1

SELECT TRIM('   Sample   ');

Result:

'Sample'

Example 2

SELECT LTRIM('   Sample   ');

Result:

'Sample   '

Example 3

SELECT RTRIM('   Sample   ');

Result:

'   Sample'

Next: SQL Length




Copyright © 2014 1keydata.com   All Rights Reserved.   Privacy Policy