UNITS

UNITS operator converts an integer expression to an INTERVAL value, expressed as a single unit of time that was specified after the UNITS keyword:

Description: UNITS%20Operator

The necessary unit of time is specified after the UNITS keyword:

integer UNITS unit

For example,

5 UNITS YEAR

8 UNITS HOUR

 

For the qualifiers YEAR, MONTH, DAY, HOUR, MINUTE and SECOND, if the left-hand expression is a decimal number, any fractional part is discarded before the UNITS operator is applied:

MAIN

DEFINE v_interval INTERVAL MONTH(3) TO MONTH

LET v_interval = 123.12345 UNITS MONTH

DISPLAY v_interval -- Displays "123"

END MAIN

However, for UNITS FRACTION, the expression can be a decimal number where the integer part is interpreted as a number of seconds, and the decimal part as the fraction of a second:

MAIN

DEFINE v_interval INTERVAL SECOND(3) TO FRACTION(5)

LET v_interval = 123.12345 UNITS FRACTION(5)

DISPLAY v_interval -- Displays "123.12345"

END MAIN

UNITS returns an INTERVAL value for a single unit of time such as DAY TO DAY, YEAR TO YEAR, or HOUR TO HOUR.

UNITS has a higher precedence than any arithmetic or boolean operator. Any left-hand arithmetic operand that includes the UNITS operator must be enclosed within parentheses:

LET dt2 = dt1 + (1*9) UNITS MONTH

UNITS operator is often used in arithmetic operations with time vales and time expressions (e.g., see the example programs, units_02_arithmetic and units_02_arithmetic_md).

For example, as the difference between two DATE values is an integer count of days rather than a value of the INTERVAL data type, we recommend you to use UNITS and to convert such differences explicitly to INTERVAL:

LET lateness = (date_due - TODAY) UNITS DAYS

Arithmetic operations with UNITS can return an invalid date. For example, this expression

LET dt1 = DATETIME (2020-01-31) YEAR TO DAY + 1 UNITS MONTH

will cause a compilation error – 1267. The result of a datetime computation is out of range. – because the expected result is February 31, 2020 and it is impossible.

 

 

Contact Us

Privacy Policy

Copyright © 2024 Querix, (UK) Ltd.