Skip to main content

TRIM function

Removes leading and/or trailing characters from a string.

Syntax

TRIM(string)
TRIM([LEADING | TRAILING | BOTH] FROM string)
TRIM([LEADING | TRAILING | BOTH] characters FROM string)

Arguments

  • string: The string to trim.

  • LEADING: Remove characters from the beginning only.

  • TRAILING: Remove characters from the end only.

  • BOTH: Remove characters from both ends (default).

  • characters: Optional. The characters to remove. Default is whitespace.

Returns

A VARCHAR with the specified characters removed.

Examples

-- Example 1: Trim whitespace from both ends (simple syntax)
> SELECT TRIM('   SQL   ');
'SQL'

-- Example 2: Trim leading whitespace only
> SELECT TRIM(LEADING FROM '   SQL   ');
'SQL   '

-- Example 3: Trim trailing whitespace only
> SELECT TRIM(TRAILING FROM '   SQL   ');
'   SQL'

-- Example 4: Trim from both ends (explicit)
> SELECT TRIM(BOTH FROM '   SQL   ');
'SQL'

-- Example 5: Trim specific characters from the beginning
> SELECT TRIM(LEADING 'abc' FROM 'abcSQLabc');
'SQLabc'

-- Example 6: Trim specific characters from the end
> SELECT TRIM(TRAILING 'abc' FROM 'abcSQLabc');
'abcSQL'

-- Example 7: Trim specific characters from both ends
> SELECT TRIM(BOTH 'abc' FROM 'abcSQLabc');
'SQL'

See Also