TRIM: Removing Leading and Trailing Occurrences

How to:

Available Languages: reporting

The TRIM function removes leading and/or trailing occurrences of a pattern within a character string.

There is a version of the TRIM function that is available only in the Maintain language. For information on this function, see TRIM: Removing Trailing Occurrences (Maintain).

Syntax: How to Remove Leading and Trailing Occurrences

TRIM(trim_where, source_string, length, pattern, sublength, output)

where:

trim_where

Alphanumeric

Is one of the following, which indicates where to remove the pattern:

'L' removes leading occurrences.

'T' removes trailing occurrences.

'B' removes both leading and trailing occurrences.

source_string

Alphanumeric

Is the string to trim enclosed in single quotation marks, or the field containing the string.

string_length

Integer

Is the number of characters in the source string.

pattern

Alphanumeric

Is the character string pattern to remove enclosed in single quotation marks.

sublength

Integer

Is the number of characters in the pattern.

output

Alphanumeric

Is the field to which the result is returned, or the format of the output value enclosed in single quotation marks.

Example: Removing Leading Occurrences

TRIM removes leading occurrences of the characters BR from the DIRECTOR field and stores the result in a field with the format A17:

TABLE FILE MOVIES
PRINT  DIRECTOR AND
COMPUTE
  TRIMDIR/A17 = TRIM('L', DIRECTOR, 17, 'BR', 2, 'A17');
  WHERE DIRECTOR CONTAINS 'BR'
END

The output is:

DIRECTOR          TRIMDIR
--------          -------
ABRAHAMS J.       ABRAHAMS J.
BROOKS R.         OOKS R.
BROOKS J.L.       OOKS J.L. 

Example: Removing Trailing Occurrences

TRIM removes trailing occurrences of the characters ER from the TITLE. In order to remove trailing non-blank characters, trailing spaces must be removed first. The TITLE field has trailing spaces. Therefore, TRIM does not remove the characters ER when creating field TRIMT. The SHORT field does not have trailing spaces. Therefore, TRIM removes the trailing ER characters when creating field TRIMS:

DEFINE FILE MOVIES
SHORT/A19 = SUBSTR(19, TITLE, 1, 19, 19, SHORT);
END
TABLE FILE MOVIES
PRINT  TITLE  IN 1  AS 'TITLE: '
       SHORT  IN 40 AS 'SHORT: ' OVER
COMPUTE
  TRIMT/A39 = TRIM('T', TITLE, 39, 'ER', 2, 'A39'); IN 1 AS 'TRIMT: '
COMPUTE
  TRIMS/A19 = TRIM('T', SHORT, 19, 'ER', 2, 'A19'); IN 40 AS 'TRIMS: ' 
WHERE TITLE LIKE '%ER'
END

The output is:

TITLE:   LEARN TO SKI BETTER         SHORT:   LEARN TO SKI BETTER
TRIMT:   LEARN TO SKI BETTER         TRIMS:   LEARN TO SKI BETT  
TITLE:   FANNY AND ALEXANDER         SHORT:   FANNY AND ALEXANDER
TRIMT:   FANNY AND ALEXANDER         TRIMS:   FANNY AND ALEXAND