REVERSE: Reversing the Characters in a String

How to:

The REVERSE function reverses the characters in a string. This reversal includes all trailing blanks, which then become leading blanks. However, in an HTML report with SET SHOWBLANKS=OFF (the default value), the leading blanks are not visible.

Syntax: How to Reverse the Characters in a String

REVERSE(length, source_string, output)

where:

length

Integer

Is the number of characters in source_string and output, or a field that contains the length.

source_string

Alphanumeric

Is the character string to reverse enclosed in single quotation marks, or a field that contains the character string.

output

Alphanumeric

Is the name of the field that contains the result, or the format of the output value enclosed in single quotation marks.

Example: Reversing the Characters in a String

In the following request against the EMPLOYEE data source, the REVERSE function is used to reverse the characters in the LAST_NAME field to produce the field named REVERSE_LAST. In this field, the trailing blanks from LAST_NAME have become leading blanks. The TRIM function is used to strip the leading blanks from REVERSE_LAST to produce the field named TRIM_REVERSE:

DEFINE FILE EMPLOYEE
REVERSE_LAST/A15 = REVERSE(15, LAST_NAME, REVERSE_LAST);
TRIM_REVERSE/A15 = TRIM('L', REVERSE_LAST, 15, ' ', 1, 'A15');
END
TABLE FILE EMPLOYEE
PRINT REVERSE_LAST TRIM_REVERSE
BY LAST_NAME
END

The output is:

LAST_NAME        REVERSE_LAST     TRIM_REVERSE
---------        ------------     ------------
BANNING                  GNINNAB  GNINNAB
BLACKWOOD              DOOWKCALB  DOOWKCALB
CROSS                      SSORC  SSORC
GREENSPAN              NAPSNEERG  NAPSNEERG
IRVING                    GNIVRI  GNIVRI
JONES                      SENOJ  SENOJ
MCCOY                      YOCCM  YOCCM
MCKNIGHT                THGINKCM  THGINKCM
ROMANS                    SNAMOR  SNAMOR
SMITH                      HTIMS  HTIMS
                           HTIMS  HTIMS
STEVENS                  SNEVETS  SNEVETS