Database Tour Documentation Contents Index

Expression Engine Functions and Operators

 

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

1. The expressions and functions are calculated "on the fly" before they are outputted to target document.

2. Names of functions and operators are case insensitive.

3. Parameters of parameterized functions may be constants (literals) or expressions.

4. String literals must be enclosed in single quotes. If a literal includes quote marks, each of them must be doubled.

5. Multiline 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.

6. Parameters of parameterized functions are strongly typed.

Incorrect
 dataset_field_val(1, Cust_Name)
Correct
 dataset_field_val(1, 'Cust_Name')
Description
  In this example, a database field name is a parameter of String (Text) type, so it should be enclosed in single quotes.

7. Expression must return only one value.

Incorrect
 Book title: dataset_field_val(1, 'TITLE')
Correct
 'Book title: ' + dataset_field_val(1, 'TITLE')
 Book title: <<<dataset_field_val(1, 'TITLE')>>>
Description
 

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 (Text) 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 (>>>).

8. 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.

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.

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.

Examples of valid expressions:

12 + abs(sqrt(5) - 2)/3 Arithmetic expression
25 - 5 >= 14 Logical expression
'File name: ' + dataset_field_val(1, 'FileName') String expression
date + 4 Date expression. Adds four days to current date. It is vaild because dates are compatible with numbers.
iif(length(Target_File_Name(1)) > 25, 'yes', 'no') Complex expression

Examples of invalid expressions:

12 / target_file_name(1) Illegal use of string function in arithmetic expression (type mismatch)
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.
5 <> False Illegal comparison of a numeric and boolean constants
power(4) Missing a second argument for power function
iif(length(Target_File_Name(1) > 25, 'yes', 'no') Missing right parenthesis for length function

Here, the expression engine operators and functions are described.

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).
- Substraction 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 below.

Database related

StepNo parameter specifies the export step, in which the function must be calculated (see note 8 above). It is used only in HTML export or reports context; in other cases it should be 1.

General
dataset_row_number( StepNo Integer, GroupLevel Integer ): Integer

Used only in reports context. Returns the current number of row in the current group of the specified GroupLevel.

GroupLevel can have the following values:

1. Group of level 1;

2. Group of level 2;

3. Group of level 3;

0. Full report;

-1. Current page.

dataset_group_number( StepNo Integer, GroupLevel Integer ): Integer

Used only in reports context. Returns the current group number of the specified GroupLevel.

GroupLevel can have the following values:

1. Group of level 1;

2. Group of level 2;

3. Group of level 3;

0. Full report (the result will be always 1);

-1. Current page (the result will be the number of the current report page).

dataset_field_val( StepNo Integer, FieldName String ): <FieldType> Returns value of field FieldName of the current dataset. The type of the returned value depends on the database field type.
dataset_param_val( StepNo Integer, ParamName String ): <ParamType> Returns value of query parameter ParamName (if the current dataset is a parameterized query). The type of the returned value depends on the parameter type.
dataset_field_is_null( StepNo Integer, FieldName String ): Boolean Returns True, when value of field FieldName of the current dataset is null, and False in other cases.
dataset_nvl( StepNo Integer, FieldName String, SubstVal <FieldType> ): <FieldType> Returns SubstVal, when value of field FieldName of the current dataset is null, and the field value in another case. The type of SubstVal must match the type of the FieldName field.
query_res( StepNo Integer, SqlText String ): <FieldType> Returns result of the one-field query, specified by SQL text SqlText. The type of the returned value depends on the database field type.

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 up 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.

GroupLevel is used in reports only (1 - calculates for group level 1; 2 - calculates for group level 2; 3 - calculates for group level 3; 0 - calculates for full report; -1 - calculates for current page). In other contexts, must be 0.

Standard (similar to aggregate functions in SQL)
dataset_row_count( StepNo Integer, GroupLevel Integer ): Integer Returns the current dataset row count (accumulating). Actually, inside the dataset rows loop, it returns the current row number.
dataset_min( StepNo Integer, GroupLevel Integer, FieldName String ): Integer Returns the lowest value in FieldName
dataset_max( StepNo Integer, GroupLevel Integer, FieldName String ): Integer Returns the highest value in FieldName
dataset_sum( StepNo Integer, GroupLevel Integer, FieldName String ): Integer Sums values in FieldName
dataset_avg( StepNo Integer, GroupLevel Integer, FieldName String ): Integer Returns the average value in FieldName
dataset_count( StepNo Integer, GroupLevel Integer, FieldName String ): Integer Returns count of non-empty values in FieldName

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.

In the functions.dtt report, you can see many examples illustrating this.

dataset_row_count_ex( StepNo Integer, GroupLevel Integer, Condition String ): Integer
dataset_min_ex( StepNo Integer, GroupLevel Integer, FieldName String, Condition String ): Integer
dataset_max_ex( StepNo Integer, GroupLevel Integer, FieldName String, Condition String ): Integer
dataset_sum_ex( StepNo Integer, GroupLevel Integer, FieldName String, Condition String ): Integer
dataset_avg_ex( StepNo Integer, GroupLevel Integer, FieldName String, Condition String ): Integer
dataset_count_ex( StepNo Integer, GroupLevel Integer, FieldName String, Condition String ): Integer

Note

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 Returns an absolute value of numeric x
frac( x Numeric ): Numeric Returns fractional part of numeric x
int( x Numeric ): Numeric Returns integer part of numeric x
round( x Numeric ): Numeric Returns the value of numeric x rounded to the nearest whole number
sqrt( x Numeric ): Numeric Returns square root of numeric x
power( x Numeric, y Numeric ): Numeric Raises numeric base x to numeric power y
exp( x Numeric ): Numeric Returns the exponential of numeric x
ln( x Numeric ): Numeric Returns the natural (base e) logarithm of numeric x
cos( x Numeric ): Numeric Returns the cosine of numeric x, where x is given in radians
sin( x Numeric ): Numeric Returns the sine of numeric x, where x is given in radians
tan( x Numeric ): Numeric Returns the tangent of numeric x, where x is given in radians
atan( x Numeric ): Numeric Returns the arc tangent of numeric x, that is, the value whose tangent is x

Date and time

date: Date Returns current date
time: Time Returns current time
add_date_time( x DateTime, y Numeric ): DateTime Adds y days to date/time x. y can be an integer or floating-point number. For example, 2.5 means two days and 12 hours.
compare_date_time( Date1 DateTime, Date2 Numeric ): Numeric Returns an integer indicating the result of comparison of the date/time Date1 and date/time Date2: 0 when Date1 = Date2, 1 when Date1 > Date2, and -1 when Date1 < Date2.
date_time_diff( Date1 DateTime, Date2 DateTime ): Numeric Returns difference in days (as floating-point number) between date/time Date1 and date/time Date2
format_date_time( x DateTime, Mask String ): String Formats a date/time x using mask Mask. For example, to display only current month, use date for the first parameter and 'm' or 'mmmm' for the second one.

HTML related (work in HTML export context)

doc_title: String Returns HTML document title as it was specified in export options
target_file_name( StepNo Integer ): String Returns full path of target HTML file as it was specified in export options. StepNo specifies the export step, in which this function must be calculated (see note 6 here).

File related

file_created( FileName String ): DateTime Returns the date and time the file FileName was created. FileName should be full file path.
file_last_modified( FileName String ): DateTime Returns the date and time the file FileName was last modified. FileName should be full file path.
file_last_accessed( FileName String ): DateTime Returns the date and time the file FileName was last accessed. FileName should be full file path.
file_size( FileName String ): Numeric Returns the size of the file FileName in bytes. FileName should be full file path.
file_version( FileName String ): Numeric Returns the version of the file FileName. FileName should be full file path.
extract_file_ext( FileName String ): String Extracts the extension of the file FileName (with period)
extract_file_name( FileName String ): String Extracts file name from the full file path FileName
extract_file_dir( FileName String ): String Extracts file path (without \ character at the end) from the full file path FileName
extract_file_path( FileName String ): String Extracts file path (with \ character at the end) from the full file path FileName
file_exists( FileName String ): Boolean Returns True if file FileName exists, False otherwise
dir_exists( DirectoryName String ): Boolean Returns True if directory DirectoryName exists, False otherwise

Miscelanious

to_number( x <AnyType> ): Numeric Converts x to number
to_string( x <AnyType> ): String Converts x to string
format_float( x Numeric, Mask String ): String Formats a numeric x using mask Mask. For example, to always display two decimal places after the floating point, use '0.00' for the second parameter.
lpad( Str String, Count Numeric, Char String ): String Adds Char characters to the left-side of a string Str. Count is the number of characters to return.
rpad( Str String, Count Numeric, Char String ): String Adds Char characters to the right-side of a string Str. Count is the number of characters to return.
iif( Condition Boolean, x <AnyType>, y <AnyType> ): <AnyType> Returns x if Condition evaluates to True, or y in another case
upper( Str String ): String Converts a string Str to upper case
lower( Str String ): String Converts a string Str to lower case
capitalize( Str String ): String Converts first characters of every word in a string Str to upper case
pretty( Str String ): String Makes the first character of string Str in upper case and the rest in lower case
length( Str String ): Integer Returns length (count of characters) of a string Str
pos( SubStr String, Str String ): Integer Returns position of substring SubStr in string Str
substr( Str String, Index Integer, Count Integer ): String Returns a substring of a string Str, containing Count characters starting at Index position
substr_count( Str String, SubStr String ): Integer Returns the number of times the substring SubStr occurs in string Str
trim( Str String ): String Trims leading and trailing spaces and control characters from a string Str
trim_left( Str String ): String Trims leading spaces and control characters from a string Str
trim_right( Str String ): String Trims trailing spaces and control characters from a string Str
string_replace( Str String, SubStr Integer, NewSubStr Integer ): String Replaces all occurancies of substring SubStr with substring NewSubStr in text Str
concat( Str1 String, Str2 Integer ): String Returns a string that is the result of concatenating string Str1 to string Str2
ordinal_number( x Integer, Language String, Case Integer, Gender Integer ): String

Returns the original number and the ordinal suffix of integer x.

Language is the language in which the result should be represented. Possible values: 'en-US' (American English), 'uk-UA' (Ukrainian).

Case (from 'A' to 'F') and Gender ('M', 'F', 'N') are for Ukrainian language only.

quantitative_numeral( x Integer, Language String, Case Integer, Gender Integer ): String

Returns the quantitative numeral of integer x.

Language is the language in which the result should be represented. Possible values: 'uk-UA' (Ukrainian).

Case (from 'A' to 'F') and Gender ('M', 'F', 'N') are for Ukrainian language only.

number_to_words( x Numeric, Language String, Options String ): String

Converts number x to its representation in words.

Language is the language in which the result should be represented. Possible values: 'en-US' (American English), 'uk-UA' (Ukrainian).

Options is a pipe separated list of options in OptionName=OptionValue format.
Allowed options:
- ShowZeroSubunits. Instructs either to include fractional part of the number if it equals to zero. Possible values: True or False. Default value: False.
- ProcessSubunits. Instructs either to convert fractional part of the number to words. Possible values: True or False. Default value: False.
- UnitsSubunitsSeparator. Specifies a separator between an integer and fractional parts of the number. Default value is a space character.
- SubunitsDimension. Specifies dimension for fractional part of the number. Default value is 2. This option is ignored when ProcessSubunits is True.
- CurrencyCode. Specifies currency code in ISO 4217 format. Currently, only the following currency codes are supported: USD, EUR, UAH. Using this option instructs to interpret the number as currency value and add main currency and fractional currency names (i.e., units and sub-units).
- Type. Specifies the type of the conversion. Possible values: regular, fraction. This option is meaningful only if Language is 'uk-UA'.
- UnitsShortened. Instructs to shorten the main currency name (units) if it is used. This option is meaningful only if Language is 'uk-UA'.
- SubunitsShortened. Instructs to shorten the fractional currency name (sub-units) if it is used. This option is meaningful only if Language is 'uk-UA'.

Examples:

Expression Result
number_to_words( dataset_field_val(1, 'pay_sum'), 'en-US', 'UnitsSubunitsSeparator= and |CurrencyCode=USD|SubunitsDimension=2|ShowZeroSubunits=true' )  
number_to_words( 3467.35, 'en-US', 'UnitsSubunitsSeparator= and |CurrencyCode=USD|ShowZeroSubunits=true' ) three thousand four hundred sixty seven dollars and 35 cents
number_to_words( 3467.35, 'en-US', 'UnitsSubunitsSeparator= and |SubunitsDimension=4|ShowZeroSubunits=true' ) three thousand four hundred sixty seven and 3500

Report related (work in reports context)

report_author: String Returns the report author's name
report_changed_by: String Returns the report modifier's name
report_database_name: String Returns database name of the report data source
report_dataset_name: String Returns dataset name (table name or SQL text) of the report data source
report_description: String Returns the report description
report_file_name: String Returns path and file name of the report template
page_left_margin: Integer Returns the page left margin in pixels
page_right_margin: Integer Returns the page right margin in pixels
page_top_margin: Integer Returns the page top margin in pixels
page_bottom_margin: Integer Returns the page bottom margin in pixels
page_number: Integer Returns number of the current page
page_count: Integer Returns page count of the report. This function requires running report in double-pass mode and may slow up large reports. It should not be used as a parameter in non-deterministic functions (e.g. database related).
page_height: Integer Returns page height in pixels
page_width: Integer Returns page width in pixels
internal_property( PropertyName String ): String

Returns value of the specified property of any report component. The PropertyName parameter should be specified in ComponentName.PropertyName form. If the ComponentName. prefix is omitted, then Report. prefix will be used.

Examples:

internal_property('Report.Author') will return the current report's author.

internal_property('PageWidth') will return the page width in pixels.

internal_property('Label12.Text') will return the text of the report's Label12 component.

internal_property('DBText1.Left') will return the horizontal coordinate of the left edge of the report's DBText1 component relative to its parent.

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