Character columns on z/OS are full of blanks. CHAR pads. VARCHAR can still hold trailing spaces you inserted. Reports need those spaces gone. Fixed-width files need them added back. DB2 gives you a family of scalar functions for both jobs: TRIM, STRIP, LTRIM, RTRIM to peel characters off the ends, and LPAD / RPAD to pad or truncate to a length. This page covers every option those functions accept.
The TRIM function removes bytes from the beginning, the end, or both ends of a string. Schema is SYSIBM.
12TRIM([ BOTH | B | LEADING | L | TRAILING | T ] [trim-constant] FROM string-expression) TRIM(string-expression) -- both ends, default strip character
Arguments:
| Option | What it does |
|---|---|
| BOTH or B | Remove the strip character from the beginning and the end (TRIM default) |
| LEADING or L | Remove only from the beginning (left) |
| TRAILING or T | Remove only from the end (right) |
123456789SELECT TRIM(:HELLO), TRIM(TRAILING FROM :HELLO) FROM SYSIBM.SYSDUMMY1; SELECT TRIM(BOTH 'x' FROM 'xxHELLOxx') AS T FROM SYSIBM.SYSDUMMY1; -- HELLO SELECT TRIM(L '0' FROM :BALANCE) FROM SYSIBM.SYSDUMMY1; -- strip leading zeros
TRIM only walks inward from the chosen end(s). It stops at the first character that is not the strip character. It will not delete an x in the middle of HExLLO.
STRIP is the same idea with argument order that looks more like a function call than the SQL-standard TRIM keyword list. IBM documents STRIP as similar to TRIM.
1STRIP(string-expression [, BOTH | B | LEADING | L | TRAILING | T [, trim-constant]])
Defaults: BOTH, and a blank (or the binary/graphic default) if you omit the strip character. If string-expression is EBCDIC mixed data, the string must contain valid mixed data. Compatibility note: older DB2 9 trim behaviour can still be requested with subsystem parameter BIF_COMPATIBILITY = V9_TRIM for LTRIM, RTRIM, and STRIP. New code should use current TRIM/STRIP semantics and valid mixed strings.
1234SELECT STRIP(LASTNAME), STRIP(LASTNAME, T), STRIP(LASTNAME, L, '*') FROM DSN8C10.EMP;
LTRIM removes characters from the beginning of a string.
1LTRIM(string-expression [, trim-expression])
Numeric arguments are implicitly cast to VARCHAR. The result is VARCHAR, VARGRAPHIC, or VARBINARY to match the source. If every character is removed, you get an empty string (length zero), not null — unless an input was null.
1234SELECT LTRIM(' ABC') AS BLANKS, -- 'ABC' LTRIM('00123DEF', '0') AS ZEROS, -- '123DEF' LTRIM('xyzHELLO', 'xyz') AS SET_TRIM -- 'HELLO' FROM SYSIBM.SYSDUMMY1;
RTRIM is the right-hand twin. It removes bytes from the end of the string based on a trim expression.
1RTRIM(string-expression [, trim-expression])
With one argument, trailing blanks disappear — the function you want on almost every CHAR column before concatenation. With two arguments, any character in trim-expression is stripped from the right until a non-member character is found.
12345SELECT RTRIM('123DEFG123', '321') FROM SYSIBM.SYSDUMMY1; -- '123DEFG' (trailing 1, 2, 3 characters removed) SELECT RTRIM(FIRSTNME) CONCAT ' ' CONCAT RTRIM(LASTNAME) AS FULLNAME FROM DSN8C10.EMP;
CHAR(12) FIRSTNME values are blank-padded. Without RTRIM, CONCAT keeps those blanks and your “full name” has a canyon of spaces in the middle. VARCHAR columns only need RTRIM if trailing blanks were actually stored.
LPAD returns a string made of the source padded on the left with a pad string (or blanks). Leading and trailing blanks in the source are significant — LPAD does not trim first.
1LPAD(string-expression, integer [, pad])
Padding occurs only if the actual length of the source is less than integer and pad is not empty. If integer is 0, the result is the empty string. If integer is less than the actual source length, the result is truncated to integer (from the left remaining / source kept from the left in the usual LPAD sense: the value is shortened to fit). The result is a varying-length string with length attribute equal to integer (or 1 when integer is 0). Source and pad must be compatible; pad is converted to the CCSID of the source when needed. FOR BIT DATA skips character conversion.
1234SELECT LPAD(EMPNO, 10, '0') AS EMP10, -- 000010 if EMPNO is 000010 already; pads if shorter LPAD(RTRIM(LASTNAME), 12, '*') AS STAR_NAME FROM DSN8C10.EMP WHERE EMPNO = '000010';
RPAD pads on the right. Same integer and pad rules as LPAD. Use RPAD when you need a fixed display width in a VARCHAR world, or when you are rebuilding a fixed-width extract without switching the column to CHAR.
1234RPAD(string-expression, integer [, pad]) SELECT RPAD(RTRIM(LASTNAME), 20, ' ') AS LAST20 FROM DSN8C10.EMP;
CHAR already pads with blanks to the declared length, so RPAD on a CHAR column is often redundant unless you first RTRIM and then pad to a different width.
A null argument yields a null result. Trimming all characters yields an empty string, which is not null. Do not write WHERE TRIM(COL) IS NULL expecting to find blank CHAR columns; those become empty strings (or remain a string of other characters). Use WHERE TRIM(COL) = '' or WHERE COL IS NULL as two separate tests.
EBCDIC mixed strings passed to LTRIM, RTRIM, or STRIP must be valid mixed data. Invalid sequences can raise SQLCODE −171 at modern APPLCOMPAT levels. Empty SI/SO pairs next to the trim character can be trimmed away under current rules. If an old program depends on DB2 9 trim quirks, that is a BIF_COMPATIBILITY conversation with your DBA, not something to copy into new SQL.
Host CHAR fields are PIC X(n) and already look padded. When you SELECT RTRIM(LASTNAME) into PIC X(15), Db2 still assigns a character string that may be shorter; the rest of the host field is typically blank-padded on assignment. When you SELECT into a VARCHAR host structure, you see the actual length after the trim. Match DCLGEN types to the function result (VARCHAR family for TRIM/LTRIM/RTRIM/LPAD/RPAD results).
123456EXEC SQL SELECT RTRIM(LASTNAME) INTO :LASTNAME-V FROM DSN8C10.EMP WHERE EMPNO = :EMPNO END-EXEC.
Imagine a name written on a sticker with extra empty squares on the left and right. TRIM and STRIP peel empty squares off both sides, or only the left, or only the right. LTRIM only peels the left; RTRIM only peels the right. If you hand them a list of junk letters, they keep peeling those junk letters from that side like picking raisins off the crust of a cookie — they stop when they hit a real ingredient. LPAD and RPAD do the opposite: they add extra squares (stars, zeros, or blanks) so the sticker is exactly as wide as you asked. If the name is already too wide, they cut it to fit.
1. What is the default end that TRIM strips when you omit BOTH, LEADING, and TRAILING?
2. How do LTRIM and RTRIM differ from TRIM when you pass a second argument?
3. What does LPAD do if the source is already longer than the integer length?
4. Which STRIP keyword means “trailing only”?
5. Why do CHAR columns often need RTRIM in comparisons or concatenation?