How can you trim a variable in CLLE??
Trimming variable in CLLE
7.5k Views Asked by AudioBubble AtThere are 4 best solutions below

Since all variables in CL are fixed length, there is no logical requirement to trim per se.
To join two value without intervening spaces, use the |<
operator, and to include a single space use |>
.
To find the length in characters excluding trailing spaces, you need to do a good ol' fashioned walk backwards on the value using %SST(&VAL &POS 1) to test each character position for a space. Something like:
DCL &LEN *DEC (15 0)
DCL &VAL *CHAR 50 VALUE('Some test data')
DCL &CHR15 *CHAR 15
CHGVAR &LEN 50
LOOP: IF (&LEN > 1 & %SST(&VAL &LEN 1)==' ') (DO)
CHGVAR &LEN VALUE(&LEN - 1)
GOTO LOOP
ENDDO
CHGVAR &CHR15 &LEN
SNDPGMMSG ('The length is' |> &CHR15) /* Can't concat decimal values */
To simply null-terminate a value to, for example, make a call to a C function:
DCL &VAL *CHAR 50 VALUE('Some test text')
DCL &VALNUL *CHAR 51 /* +1 for the null */
DCL &NULL *CHAR 1 VALUE(X'00')
CHGVAR &VALNUL VALUE(&VAL |< &NULL)
EDIT 2012-07-19*
In some character sets !
is used instead of |
. There is also the CCSID independent *CAT operation that can be used instead. See IBM's website here and here.

In releases before %TRIM(), %TRIML() and %TRIMR() were available, ILE CL could use the 'triml' C library function:
dcl &lPath *int value( 0 )
callprc 'triml' ( +
&Dir +
( ' ' *byval ) +
) +
rtnval( &lPath )
The proc is found in *SRVPGM QC2UTIL1.
In both OPM and ILE CL, I've often used:
dcl &lmsgtxt1 *dec ( 5 0 ) value( 0 )
rtvmsg msgid( cpf9897 ) msgf( QSYS/QCPFMSG ) +
msgdta( &SQLStm ) +
msglen( &lmsgtxt1 )
In both cases, the resulting length can then be used in a %SST() expression to effect the actual trimming.
No standard function TRIM is available in CLLE. However,