TRIM: Removing Leading or Trailing Characters (SQL)

How to:

The TRIM function removes leading and/or trailing characters from a character string. The character to be removed may be specified. If no character is specified, the space character is assumed. Whether to remove leading and/or trailing characters may be specified. Without this specification, both leading and trailing appearances of the specified character are removed.


Top of page

x
Syntax: How to Remove Leading or Trailing Characters
TRIM(arg)
TRIM(trim-where [trim-char] FROM arg)
TRIM(trim-char FROM arg)

where:

arg

character string

Is the source string value to be trimmed.

trim-where

Value may be LEADING, TRAILING or BOTH. Indicates where characters will be removed. If not specified, BOTH is assumed.

trim-char

character string

Is the character to be removed. If not specified, the space character is assumed.

This function returns a varying character string. The data type of the result has a length equal to that of the input argument (although the value may be shorter).



Example: Removing Leading or Trailing Characters

TRIM removes leading and/or trailing characters. This example,

TRIM('  ABC  ')

returns ABC.

This example,

TRIM(LEADING FROM '  ABC  ')

returns 'ABC  '.

This example,

TRIM(TRAILING FROM '  ABC  ')
TRIM(BOTH 'X' FROM 'XXYYYXXX') = ('YYY')

returns '  ABC'

This example,

TRIM(BOTH 'X' FROM 'XXYYYXXX')

returns YYY.


iWay Software