TRIM function

TRIM is a string manipulation function that manipulates all string data types (BIT, BLOB, and CHARACTER), and removes trailing and leading singletons from a string.

Syntax

Read syntax diagramSkip visual syntax diagramTRIM( trim_singletonBOTHLEADINGTRAILINGtrim_singleton FROM  source_string)

TRIM returns a new string of the same type as source_string, in which the leading, trailing, or both leading and trailing singletons have been removed. The term singleton refers to a single part (BIT, BYTE, or CHARACTER) within a string of that type.

The singleton can contain a list of multiple characters to be trimmed from the source string.

If trim_singleton is not specified, a default singleton is assumed. The default singleton depends on the data type of source_string:
Character ' ' (space)
BLOB X'00'
Bit B'0'

If any parameter is NULL, the result is NULL.

It is often unnecessary to strip trailing blanks from character strings before comparison, because the rules of character string comparison mean that trailing blanks are not significant.

The following examples illustrate the behavior of the TRIM function:
TRIM(TRAILING 'b' FROM 'aaabBb')
returns 'aaabB'.
TRIM('  a  ')
returns 'a'.
TRIM(LEADING FROM '  a  ')
returns 'a '.
TRIM('b' FROM 'bbbaaabbb')
returns 'aaa'.
An example of using a multiple character singleton is:
DECLARE input1 CHARACTER 'testmgktest'; 
SET OutputRoot.XMLNSC.Top.Out1 = TRIM( 'ste' FROM input1); 
The preceding code produces the output message:
<Top><Out1>mgk</Out1></Top>
An example of using a multiple character singleton to remove leading and trailing white space characters is:
DECLARE whiteSpace CONSTANT CHARACTER CAST( X'090D0A20' AS CHAR CCSID 1208);
 /* tab, cr, lf, space */ 
DECLARE input2 CHARACTER 'smith'; 
                            
SET input2 = whiteSpace || input2 || whiteSpace; 
SET OutputRoot.XMLNSC.Top.Out2 = TRIM( whiteSpace FROM input2);
The preceding code produces the output message:
<Top><Out2>smith</Out2></Top>