Database Tour Documentation Contents Index

Expression Engine Functions and Operators

 

Basic Information

Database Tour Pro expression engine allows you to execute Pascal-like expressions, which can be embedded in dynamic elements during export (for example, in field mappings or when exporting database data to HTML using HTML templates) and in reports.

The expressions can consist of functions (nesting of any depth), arithmetical and logical operators, and literals (numeric, string, or boolean). Expression returns one value of numeric, string, or boolean type.

Notes

The expressions and functions are calculated "on the fly" before they are outputted to target document.
Names of functions and operators are case insensitive.
Parameters of parameterized functions may be constants (literals) or expressions.
String literals must be enclosed in single quotes. If a literal includes quote marks, each of them must be doubled.
Multi-line string literals are not allowed. If you want to pass big text as parameter, you can split it on less separate string literals and concatenate them using + operator.
Parameters of parameterized functions are strongly typed.

Incorrect

Incorrect expression dataset_field_val(1, Cust_Name)

Correct

Incorrect expression dataset_field_val(1, 'Cust_Name')

Explanation: In this example, a database field name is a parameter of String (Text) type, so it should be enclosed in single quotes.

Expression must return only one value.

Incorrect

Incorrect expression Book title: dataset_field_val(1, 'TITLE')

Correct

Incorrect expression 'Book title: ' + dataset_field_val(1, 'TITLE')

Incorrect expression Book title: <<<dataset_field_val(1, 'TITLE')>>>

Explanation: To produce one value, the number of operands in a String (Text) expression must equal the number of operators plus one. In other words, there should be an operator between each two operands in a valid String expression.

Another solution is to separate dynamic parts of the expression. In reports context, dynamic parts begin with three opening triangle brackets (<<<) and end with three closing triangle brackets (>>>).

In some functions, there is a StepNo parameter. Its meaning depends on the context in which the function is used:

Exporting to HTML using template context. HTML template can be filled with dynamic data in more than one step. For example, if your HTML document consists of several tables, which must be filled with database data from different datasets, you can fill them only in several export steps (usually, one dataset per step). That's why database related and some other functions have a StepNo parameter. When exporting a dataset to HTML using HTML template, it is also possible to specify an export step number (it is 1 by default). During the export procedure, only those expressions will be calculated where the StepNo parameter of expression function(s) is not present or matches with a step number, specified in export options. For example, the database_field_val(2, 'payment_sum')/100 expression will be calculated only in the second export step.

Reports context. StepNo parameter specifies the step (pass) in which the function must be calculated. Possible values: 1 or 2. When it equals to 1, the corresponding function will be calculated immediately when the expression is processed. When it equals to 2, report will run in double-pass mode and the corresponding function will be calculated only in the second pass of the report processing, i.e. after the report dataset reaches the last record. Using database functions in the second pass is useful, for example, when it is needing to put the report totals in the report or group headers.

Other contexts. Must be 1.

Combining different StepNo parameters in one expression (for example, database_field_val(1, 'total_sum') + database_field_val(2, 'payment_sum')) is possible, but such expression should be written so that functions with smaller StepNo will be calculated earlier.

Examples of valid expressions

Correct expression 12 + abs(sqrt(5) - 2)/3 {Arithmetic expression}

Correct expression 25 - 5 >= 14 {Logical expression}

Correct expression 'File name: ' + dataset_field_val(1, 'FileName') {String expression}

Correct expression date + 4 {Date expression. Adds four days to current date. It is valid because dates are numbers internally.}

Correct expression iif(length(Target_File_Name(1)) > 25, 'yes', 'no') {Complex expression}

Examples of invalid expressions

Incorrect expression 12 / target_file_name(1) {Illegal use of string function in arithmetic expression (type mismatch)}

Incorrect expression concat(cos(dataset_field_val(1, 'FuncParam')), ' is larger') {Illegal use of floating point number as an argument of string concatenation. There must be explicit type casting using to_string function.}

Incorrect expression 5 <> False {Illegal comparison of a numeric and boolean constants}

Incorrect expression power(4) {Missing a second argument for power function}

Incorrect expression iif(length(Target_File_Name(1) > 25, 'yes', 'no') {Missing right parenthesis for length function}

Operators

Arithmetic

Work with numbers and return numbers (except + operator, which can be used for string concatenation).

+ Addition of numbers. Can be also used for concatenating string operands (as an alternative for concat function).
- Subtraction of numbers
* Multiplication of numbers
/ Division of numbers
^ Raises left operand to power, specified by the right operand (as an alternative for power function)
div Integer division. It is division in which the fractional part (remainder) is discarded.
mod Remainder of integers. It divides two numbers and returns only the remainder.

Logical

Work with boolean constants or expressions of any kind, which return a boolean value, and return boolean True or False.

not Logical NOT. Returns False if its single operand can be converted to True; otherwise, returns True.
and Logical AND. Returns True if both operands are True; otherwise, returns False.
or Logical OR. Returns True if either operand is True; if both are False, returns False.
xor Exclusive OR. Returns True if one operand is True and the other is False; otherwise, returns False.
= Equality
<> Inequality
> Greater than
>= Greater than or equal
< Less than
<= Less than or equal

Functions

Use functions to calculate values. Each function returns one value. Function parameters can be constants or expressions (including recursive function calls), unless otherwise noted in function descriptions.

Database related

General

dataset_row_number(
  StepNo Integer, 
  GroupLevel Integer 
): Integer
dataset_group_number(
  StepNo Integer, 
  GroupLevel Integer 
): Integer
dataset_field_val(
  StepNo Integer,
  FieldName String | FieldIndex Integer
): <FieldType>
dataset_field_is_null(
  StepNo Integer,
  FieldName String | FieldIndex Integer
): Boolean
dataset_nvl(
  StepNo Integer,
  FieldName String | FieldIndex Integer,
  SubstVal <FieldType>
): <FieldType>
dataset_field_exists(
  StepNo Integer,
  FieldName String
): Boolean
dataset_param_val(
  StepNo Integer,
  ParamName String
): <ParamType>
query_res(
  StepNo Integer,
  SqlText String
): <FieldType>

Statistical (aggregate)

These functions perform calculation on current dataset data. Unlike other functions, statistical functions accumulate data of each record of dataset field, therefore they need more resources and their using (especially for conditional functions) may slow down processing for large datasets.

It is strongly recommended to use these functions only inside or after loop(s) through dataset rows, otherwise they will return inadequate values.

Standard (similar to aggregate functions in SQL)

dataset_row_count(
  StepNo Integer,
  GroupLevel Integer
): Integer
dataset_min(
  StepNo Integer,
  GroupLevel Integer,
  FieldName String
): Numeric
dataset_max(
  StepNo Integer,
  GroupLevel Integer,
  FieldName String
): Numeric
dataset_sum(
  StepNo Integer,
  GroupLevel Integer,
  FieldName String
): Numeric
dataset_avg(
  StepNo Integer,
  GroupLevel Integer,
  FieldName String
): Numeric
dataset_count(
  StepNo Integer,
  GroupLevel Integer,
  FieldName String
): Integer

Conditional

These functions work like standard statistical functions, but with one important difference: they check Condition for each row, and calculate only if it evaluates to True. It is important to write the Condition parameter in these functions as String constant (i.e. enclosed in single quotes) or a string deterministic expression (see below).

The parameters of these functions must be constants or deterministic expressions (i.e. return the same result any time they are called with a specific set of input values). The Condition parameter can use also nondeterministic expressions, but, as noted above, it should be written as string literal or string deterministic expression.

dataset_row_count_ex(
  StepNo Integer,
  GroupLevel Integer,
  Condition String
): Integer
dataset_min_ex(
  StepNo Integer,
  GroupLevel Integer,
  FieldName String,
  Condition String
): Numeric
dataset_max_ex(
  StepNo Integer,
  GroupLevel Integer,
  FieldName String,
  Condition String
): Numeric
dataset_sum_ex(
  StepNo Integer,
  GroupLevel Integer,
  FieldName String,
  Condition String
): Numeric
dataset_avg_ex(
  StepNo Integer,
  GroupLevel Integer,
  FieldName String,
  Condition String
): Numeric
dataset_count_ex(
  StepNo Integer,
  GroupLevel Integer,
  FieldName String,
  Condition String
): Integer
It is not recommended to use nested calls of statistical functions (i.e. when Condition contains statistical functions calls), because the program cannot guarantee the correct result of such calls.
But if you do this, try to avoid cyclic field references in these functions because this may cause unexpected results of expressions. For example, using FieldName inside Condition is a cyclic reference and should be avoided.

Examples of correct using of conditional statistical functions:

dataset_sum_ex(1, 0, 'PaymentSum', 'dataset_field_val(1, ''CustNo'') > 1000')

(this expression will sum data from PaymentSum field, when value of numeric field CustNo is larger then 1000)

dataset_count_ex(1, 0, 'CustNo', 'dataset_field_val(1, ''Paid'')')

(this expression will return count of values from field CustNo, when value of boolean field Paid is True)

dataset_sum_ex(1, 0, 'BillSum', 'dataset_sum_ex(1, 0, ''OldBillSum'', ''dataset_field_val(1, ''''CustNo'''') > 0'') > 0')

(this function will return sum of values from field BillSum, when conditional sum of field OldBillSum is larger than 0)

Examples of incorrect using of conditional statistical functions:

dataset_count_ex(1, 0, 'CustNo', 'yes')

(error: not boolean constant cannot be used as condition)

dataset_sum_ex(1, 0, 'BillSum', 'dataset_sum_ex(1, 0, ''OldBillSum'', ''dataset_sum_ex(1, 0, ''''BillSum'''', True) > 0'') > 0')

(error: cyclic reference for field BillSum)

Mathematical

abs(
  x Numeric
): Numeric
frac(
  x Numeric
): Numeric
int(
  x Numeric
): Numeric
round(
  x Numeric
): Integer
sqrt(
  x Numeric
): Numeric
power(
  x Numeric,
  y Numeric
): Numeric
exp(
  x Numeric
): Numeric
ln(
  x Numeric
): Numeric
cos(
  x Numeric
): Numeric
sin(
  x Numeric
): Numeric
tan(
  x Numeric
): Numeric
atan(
  x Numeric
): Numeric

Date and time

date: DateTime
time: DateTime
add_date_time(
  x DateTime,
  y Numeric
): DateTime
compare_date_time(
  Date1 DateTime,
  Date2 DateTime
): Integer
date_time_diff(
  Date1 DateTime,
  Date2 DateTime
): Numeric
format_date_time(
  x DateTime,
  Mask String
): String

Export related (work in data export context)

target_file_name(
  StepNo Integer
): String

HTML export related (work in data export to HTML context)

doc_title: String

File related

file_created(
  FileName String
): DateTime
file_last_modified(
  FileName String
): DateTime
file_last_accessed(
  FileName String
): DateTime
file_size(
  FileName String
): Numeric
file_version(
  FileName String
): String
extract_file_ext(
  FileName String
): String
extract_file_name(
  FileName String
): String
extract_file_dir(
  FileName String
): String
extract_file_path(
  FileName String
): String
extract_file_text(
  FileName String
): String
file_exists(
  FileName String
): Boolean
dir_exists(
  DirectoryName String
): Boolean

Miscellaneous

to_number(
  <AnyType> String
): Numeric
to_string(
  <AnyType> String
): String
format_float(
  x Numeric,
  Mask String
): String
lpad(
  Str String,
  Count Numeric,
  Char String
): String
rpad(
  Str String,
  Count Numeric,
  Char String
): String
iif(
  Condition Boolean,
  x <AnyType>,
  y <AnyType>
): <AnyType>
upper(
  Str String,
): String
lower(
  Str String,
): String
capitalize(
  Str String,
): String
pretty(
  Str String,
): String
length(
  Str String,
): Integer
pos(
  SubStr String,
  Str String
): Integer
substr(
  Str String,
  Index Integer,
  Count Integer
): String
substr_count(
  Str String,
  SubStr String
): Integer
trim(
  Str String,
): String
trim_left(
  Str String,
): String
trim_right(
  Str String,
): String
string_replace(
  Str String,
  SubStr String,
  NewSubStr String
): String
concat(
  Str1 String,
  Str2 String
): String
rgb(
  Red Integer,
  Green Integer,
  Blue Integer
): String
ordinal_number(
  x Integer,
  Language String,
  Case String,
  Gender String
): String
quantitative_numeral(
  x Integer,
  Language String,
  Case String,
  Gender String
): String
number_to_words(
  x Numeric,
  Language String,
  Options String
): String

Report related (work in reports context)

report_author: String
report_changed_by: String
report_database_name: String
report_dataset_name: String
report_description: String
report_file_name: String
page_left_margin: Integer
page_right_margin: Integer
page_top_margin: Integer
page_bottom_margin: Integer
page_number: Integer
page_count: Integer
page_height: Integer
page_width: Integer
internal_property(
  PropertyName String
): String

Obsolete report functions

When converting expressions with obsolete functions from your old reports, please consider the rules at the top of this topic.

GROUPRECORDNUMBER This function is no longer supported. Use dataset_row_number instead
NUMTOWORDS This function is no longer supported. Use number_to_words instead
DATASETNAME Use report_dataset_name instead
DATABASENAME Use report_database_name instead
FILENAME Use report_file_name instead
PAGEHEIGHT Use page_height instead
PAGEWIDTH Use page_width instead
LEFTMARGIN Use page_left_margin instead
RIGHTMARGIN Use page_right_margin instead
TOPMARGIN Use page_top_margin instead
BOTTOMMARGIN Use page_bottom_margin instead
PAGENUMBER Use page_number instead
RECORDNUMBER Use dataset_row_number instead
PAGERECORDNUMBER Use dataset_row_number instead
GROUP1RECORDNUMBER Use dataset_row_number instead
GROUP2RECORDNUMBER Use dataset_row_number instead
GROUP3RECORDNUMBER Use dataset_row_number instead
RECORDCOUNT Use record_count instead
REPORTDESCRIPTION Use report_description instead
REPORTAUTHOR Use report_author instead
PAGECOUNT Use page_count instead
MIN Use dataset_min instead
MAX Use dataset_max instead
SUM Use dataset_sum instead
AVERAGE Use dataset_avg instead
COUNT Use dataset_count instead
MINEX Use dataset_min_ex instead
MAXEX Use dataset_max_ex instead
SUMEX Use dataset_sum_ex instead
AVERAGEEX Use dataset_avg_ex instead
COUNTEX Use dataset_count_ex instead
FILESIZE Use file_size instead
FIELDVAL Use dataset_field_val instead
PARAMVAL Use dataset_param_val instead
ISNULL Use dataset_is_null instead
FIELDEXISTS Use dataset_field_exists instead
NVL Use dataset_nvl instead
QUERYRES Use query_res instead
STR Use to_string instead
TRIMLEFT Use trim_left instead
TRIMRIGHT Use trim_right instead
ORDINALNUMBER Use ordinal_number instead
QUANTITATIVENUMERAL Use quantitative_numeral instead
FILECREATED Use file_created instead
FILELASTMODIFIED Use file_last_modified instead
FILELASTACCESSED Use file_last_accessed instead
EXTRACTFILENAME Use extract_file_name instead
EXTRACTFILEEXT Use extract_file_ext instead
EXTRACTFILEDIR Use extract_file_dir instead
EXTRACTFILEPATH Use extract_file_path instead
REPLACE Use string_replace instead
ADDDATETIME Use add_date_time instead
COMPAREDATETIME Use compare_date_time instead
DATETIMEDIFF Use date_time_diff instead
FORMATDATETIME Use format_date_time instead
SUBSTRCOUNT Use substr_count instead