Combining Two Alphanumeric Fields Into a New Field

In the previous section, you created a new numeric field by performing a simple calculation on two numeric fields in the data source. You can also create new alphanumeric fields.

For example, you can combine the data in two or more alphanumeric fields into a new field. This is called concatenation. There are two forms of concatenation, weak and strong. You use weak concatenation when you want to include trailing blanks in the field value to which you are concatenating. Trailing blanks occur when a field value is shorter than the field length. The blanks occur at the end of the value, trailing behind it. When you want to eliminate blanks at the end of the value, you use strong concatenation.

You use a solid bar (|) or a broken vertical bar (¦) to indicate concatenation. A single bar means weak concatenation, where the entire field value, including blanks, is used. A double bar (| |) means strong concatenation, where blanks at the end of the first data value are discarded. It is good coding practice to include a space before and after the concatenation bar.

To show how this works, first combine the fields FIRST_NAME and LAST_NAME into one field called FULL_NAME, using weak concatenation. You use a format of A30 to account for the alphanumeric nature of the data (first names and last names) and to allow enough space in the field to fit the two names.

Issue the following request:

TABLE FILE EMPLOYEE
PRINT CURR_JOBCODE AND
COMPUTE FULL_NAME/A30= FIRST_NAME | LAST_NAME;
END

Run the request. The output is:

The names retrieved from the FIRST_NAME and LAST_NAME fields have been combined into the field FULL_NAME. Notice two things about the temporary field definition:

  1. The characters /A30 declare that FULL_NAME is an alphanumeric field 30 characters long. Remember that the default is a numeric format for temporary fields. Because the fields used in this expression are alphanumeric, FULL_NAME must also be defined as an alphanumeric field. A length of 30 characters provides room for the combined values from both FIRST_NAME and LAST_NAME.
  2. FIRST_NAME is 10 characters long. For names that are shorter than this, blank spaces fill the rest of the field. Using the single vertical bar (|) keeps these spaces in FULL_NAME.

If you want only one blank space between the first names and last names, you cannot use the double vertical bar (| |) because strong concatenation discards all trailing blanks. The two (2) names will simply be stuck together. Instead, you first append a blank to the start of LAST_NAME using weak concatenation. Then you concatenate the result to FIRST_NAME, using strong concatenation to discard the trailing blanks from the end of that field. Use parentheses ( ) around the blank space and LAST_NAME to perform this concatenation first, then concatenate FULL_NAME to the result.

Issue the following request:

TABLE FILE EMPLOYEE
PRINT CURR_JOBCODE
COMPUTE FULL_NAME/A30=FIRST_NAME || (' '| LAST_NAME);
END

Run the request. The output is:

The field FULL_NAME now appears as you want it to, with one blank space between the names. The parentheses around the blank space and LAST_NAME perform this concatenation first, then concatenate FULL_NAME to the result.

In this example, the field FULL_NAME appears to the right of the field CURR_JOBCODE, because you can only write the COMPUTE command after you have specified the verbs and field names for the request. Since you used COMPUTE, you cannot put FULL_NAME before the PRINT command or use it to sort the report.

WebFOCUS

Feedback