donderdag 19 juni 2014

Crystal Reports: On SQL Expression Fields

This is based on an article from the SAP site:

I tried to add some more examples to it.

SQL Expression Field definition

SQL Expression fields are similar to formula fields, but they are written in Structured Query Language (SQL). They are useful in optimizing report performance because the tasks they execute are performed on the database server.

You can use SQL Expression fields to perform pre-defined functions on database fields. The list of available functions depends on the type of database in use. This list is available in the Function Tree of the SQL Expression Editor dialog box.

NOTE: If Crystal Reports recognizes the function name it will turn blue in the SQL Expression Editor dialog box. Additional database functions may be available to you, depending on your database, but they will not turn blue.

SQL Expression field versus a Crystal Reports formula field

Often an SQL Expression field and a Crystal Reports formula field can accomplish the same goal. For example, a report design requirement is to display the first 3 letters of the first name of an employee.

A formula field or an SQL Expression field can fulfill this requirement. The advantage of the SQL Expression field is that the processing of the request will be done on the database server instead of in the Crystal Reports Designer. This results in a faster processing time.

SQL Expression for SQL Server:

{fn LEFT("Employee"."First Name",3)}

Crystal Reports formula:

left({Employee.First Name},3)

SQL Expression Fields and SELECT Statements

Using a SELECT statement in a SQL Expression field is not supported. Generally, an SQL Expression field cannot contain a SELECT statement because Crystal Reports can only process one SELECT statement per main report. If a SELECT statement is included in an SQL Expression field, an error message, similar to the following, may appear:

 The exact wording of the message depends upon the database in use. In this case, I used SQL Server.
Although SQL Expressions with SELECT statements are not supported, they can work if they only return a single value.  For example, Maximum, Minimum and Count are functions that return a single value.

It appears to be neceassary to enclose the SELECT statement with parentheses.  For example, a Microsoft SQL Server requires the SELECT statement to be enclosed in parentheses and so does Microsoft Access.

Example for the SQL Server error message when you leave the parenthesis out:

Example with the parenthesis:
FROM orders
WHERE CustomerId = "Customers"."CustomerID"

Same example for Microsoft Access:

FROM orders
WHERE CustomerId = `Customers`.`CustomerID`

In both cases, the Show SQL Query box will look something like this:

Database Functions Not Pre-Defined in Crystal Reports

Database functions that are not pre-defined in Crystal Reports can be used in SQL Expression fields.
For example, in case of the SQL Server:


LOWER is not a pre-defined function in Crystal Reports and, therefore, will not turn blue in the SQL Expression Editor. However, the syntax will be accepted and you can now insert this field into your report.
Another example for the SQL Server:
DATEPART("WW", "Orders"."OrderDate")
Another example for Microsoft Access:
DATEPART("WW", `Orders`.`OrderDate`,2,2)

In both cases the Show SQL Query box will look something like this:


You can not use parameters in an SQL Expression field. In case you want to use them, you have to switch to Commands.
Een reactie posten