Conversion functions

Conversion functions change a value from one data type to another. Each function has the name of the data type that it gives: text, number, indicator, date and time, and also day-time-duration and year-month-duration for the two kinds of duration. EFX never converts a value by itself. So, to combine values of different types, for example to put a number into a text, you use these functions.

For an overview of the conversions that exist, see Type conversion.

text

Converts a number, an indicator, a date, a time or a duration to text.

Syntax
text(<number>) → text
text(<indicator>) → text
text(<date>) → text
text(<time>) → text
text(<duration>) → text
<number>

number. The number to convert.

<indicator>

indicator. The indicator to convert.

<date>

date. The date to convert.

<time>

time. The time to convert.

<duration>

duration. The duration to convert.

The result is the standard text form of the value:

  • A number has a full stop as decimal separator, no grouping separators, and no zeros that do not change its value: 2.50 becomes 2.5. A number read from a field is written in exponent notation when its absolute value is 1 000 000 or more, or less than 0.000001 (for example 1.234567E6). Numbers read from fields are stored with limited precision, so a calculated value can differ slightly from the exact result, for example 0.30000000000000004 instead of 0.3.

  • An indicator becomes true or false. For an indicator field, the value is written as it appears in the notice.

  • A date becomes YYYY-MM-DD, followed by its time zone when it has one (2024-03-01+01:00, 2024-03-01Z).

  • A time becomes hh:mm:ss, followed by its time zone when it has one (09:30:00Z).

  • A duration becomes its ISO 8601 form, in its simplest form. For example, a duration of 18 months becomes P1Y6M, a duration of two weeks becomes P14D, and a duration of 36 hours becomes P1DT12H.

If the argument has no value, for example a field that is not present in the notice, the result is an empty text.

string is another name for this function, kept from EFX-1: string(5) and text(5) are the same.

To present numbers, dates and times to readers in a template, use the formatting functions instead: format-number, format-short and the others.

Restrictions

  • The argument cannot be a text. A text field or a text literal as argument is an error.

Examples

concat('Lot ', text(BT-00-Integer))

Builds a text such as Lot 3 from an integer field.

text(BT-00-StartDate)

Gives the date as text, for example 2024-03-01.

string(count(BT-13-Text))

Uses the name string. The result is the number of values of the repeatable field BT-13-Text, as text.

From the SDK (validation/rules.efx)
WITH BT-52-Lot
  ASSERT text(BT-52-Lot) in [...indicator]
    AS ERROR R-IRS-3HS
    FOR BT-52-Lot
    IN *;

A rule that checks the value of an indicator field against the indicator codelist.

EFX-1 had only string(<number>). EFX-2 adds the name text, and accepts indicators, dates, times and durations. In EFX-1, the number was written with the decimal format set for the translation: with up to ten decimal places, and with a decimal comma when the format said so. In EFX-2, the result is always the standard form described above.

See also: number, concat

number

Converts a text or an indicator to a number.

Syntax
number(<text>) → number
number(<indicator>) → number
<text>

text. The text to read as a number.

<indicator>

indicator. The indicator to convert.

number reads a text as a decimal number: an optional sign, digits, an optional full stop followed by digits, and an optional exponent (1.5E3). It ignores spaces at the start and at the end. Any other text gives NaN ("not a number"), also an empty text, a text with a decimal comma (1,5) and a text with a grouping separator (1 000). NaN is a special number value. It is not equal to any number, not even to itself, and calculations that involve it give NaN. If the text has no value, the result is also NaN.

An indicator gives 1 when it is TRUE, and 0 when it is FALSE.

Restrictions

  • The argument cannot be a number. A number field as argument is an error.

Examples

number('2.5') * 2

Gives 5.

number(substring-after(BT-00-Identifier, '-')) > 100

Reads the part of an identifier after the first hyphen as a number, and compares it.

sum(for number:$n in BT-13-Number return number($n > 100))

Counts how many values of the repeatable field BT-13-Number are greater than 100. Each comparison gives an indicator, which number converts to 1 or 0.

EFX-1 accepted only a text argument. The indicator argument is new in EFX-2.

See also: text, indicator, Number

indicator

Converts a number to an indicator.

Syntax
indicator(<number>) → indicator
<number>

number. The number to convert.

The result is FALSE when the number is zero or NaN, and TRUE for any other number, positive or negative. If the number has no value, the result is FALSE.

Restrictions

  • The argument must be a number. An indicator field as argument is an error.

Examples

indicator(count(BT-13-Text))

Gives TRUE when the repeatable field BT-13-Text has at least one value.

indicator(BT-00-Number)

Gives TRUE when the number field holds a value other than zero.

indicator is new in EFX-2.

See also: number, Indicator

date

Converts a text to a date.

Syntax
date(<text>) → date
<text>

text. A date written as YYYY-MM-DD, optionally followed by a time zone (Z, +hh:mm or -hh:mm).

The text must be a valid date in the form shown: four digits for the year, and two digits each for the month and the day (2024-03-01, not 2024-3-1). date ignores spaces at the start and at the end. A text that is not a valid date causes an error when the expression is evaluated. If the text has no value, the result has no value.

Restrictions

  • The argument must be a text. A date field as argument is an error, because a date needs no conversion.

Examples

date('2024-03-01')
year(date(BT-00-Text))

Reads a date from a text field and takes its year.

date(concat(text(BT-00-Integer), '-12-31'))

Builds the date of the last day of the year held in an integer field.

See also: Date literal, text, Date

time

Converts a text to a time.

Syntax
time(<text>) → time
<text>

text. A time written as hh:mm:ss, optionally with decimal seconds and a time zone (Z, +hh:mm or -hh:mm).

The text must be a valid time in the form shown, with two digits each for the hours, minutes and seconds. A time without seconds (09:30) is not valid. time ignores spaces at the start and at the end. A text that is not a valid time causes an error when the expression is evaluated. If the text has no value, the result has no value.

Restrictions

  • The argument must be a text. A time field as argument is an error.

Examples

time('09:30:00')
hours(time(concat(BT-00-Text, ':00')))

Adds the seconds to a time written as hh:mm in a text field, then takes the hours.

See also: Time literal, text, Time

day-time-duration

Converts a text to a duration in days, hours, minutes and seconds.

Syntax
day-time-duration(<text>) → duration
<text>

text. A duration in ISO 8601 form that uses only days (D), hours (H), minutes (M after T) and seconds (S), for example P3D, PT12H or P1DT6H30M. A minus sign in front makes the duration negative.

Unlike a duration literal, the text can combine several units, and it can include hours, minutes and seconds. The text cannot contain years, months or weeks: P1Y or P2W causes an error when the expression is evaluated. For two weeks, write P14D. If the text has no value, the result has no value.

Restrictions

  • The argument must be a text. A duration field as argument is an error.

Examples

day-time-duration('P1DT12H')

A duration of one day and twelve hours.

add-duration(BT-00-StartDate, day-time-duration(concat('P', text(BT-00-Integer), 'D')))

Adds to a date the number of days held in an integer field.

year-month-duration

Converts a text to a duration in years and months.

Syntax
year-month-duration(<text>) → duration
<text>

text. A duration in ISO 8601 form that uses only years (Y) and months (M), for example P2Y, P18M or P1Y6M. A minus sign in front makes the duration negative.

Unlike a duration literal, the text can combine years and months. It cannot contain days, weeks or smaller units: P3D causes an error when the expression is evaluated. If the text has no value, the result has no value.

Restrictions

  • The argument must be a text. A duration field as argument is an error.

Examples

year-month-duration('P1Y6M')
months(year-month-duration('P1Y6M'))

Gives 18.