The following guide describes how to create a Crystal Report which check if a field is blank (NULL or empty) and if it is use an alternative field.
This could be helpful if your database table has two fields for similar information and there are inconsistencies with the data entry – for example if there is a street address POST CODE and a postal address POST CODE however only one will contain data.
In this example the database field containing the post codes will be ‘Postal_Address_PostCode’ and ‘Street_Address_PostCode’. We will be using the ‘SQL Expression Fields’ feature to query the database.
Please note, this guide was created for Crystal Reports 2008 (version 12), however the same process should work with other versions of the Crystal Reports application.
- Open the report in Crystal Reports
- Using the Field Explorer, right-click on ‘SQL Expression Fields’ and select ‘New’
- Enter in the name for the field, for example SelectPostcode
- Click ‘OK’
- In the formula field enter in the following code (replacing the database values with your own)
-
{fn IFNULL("databaseTable"."Postal_Address_PostCode", "databaseTable"."Street_Address_PostCode")}
-
Click ‘Save and close’ to save the changes
- Insert the SelectPostcode field into the report, format and create a heading as required
- Preview or run the report. The report will now list either the street or postal post code for each record. If the street post code database field is empty it will use the postal post code.