Writing Expressions : Manipulating string data : Combining values from multiple fields
 
Combining values from multiple fields
Each field in a database often represents a single piece of information. For example, a customer table might contain these fields: customerFirstname, customerLastname, addressLine_1, addressLine_2, city, state, zip, and country.
You can create a customer report that uses data from all these fields by dragging each field to a table cell. The generated report, however, does not look professional because the spaces between the pieces of data are uneven, as shown in Figure 11‑6.
Figure 11‑6 Report with separate field values
The solution is to combine, or concatenate, the first and last names and place the concatenated name in a single table cell. Similarly, concatenate all the address-related fields and place the full address in a single table cell. In JavaScript, concatenate string values using the + operator.
For the name, add a literal space (" ") between the name fields so that the first and last name values do not run together. For the address, add a comma and space between all the fields, except between state and zip. For these fields, add only a space between them.
For this example, use the following expression to display the full customer name:
row["customerFirstname"] + " " + row["customerLastname"]
Use the following expression to display the full address:
row["addressLine1"] + ", " + row["addressLine2"] + ", " + row["city"] + ", " + row["state"] + " " + row["zip"] + ", " + row["country"]
The report now looks like the one shown in Figure 11‑7.
Figure 11‑7 Report with combined field values
Several addresses display the word null because the addressLine2 field contains no data. In a database, a null value means no value was supplied. In cases where you concatenate fields that might contain no data, you need to remove the word null from the returned string value. This task is described in the next section.