Cast Expression

Syntax

cast_expression ::= CAST "(" expression AS sequence_type ")"

For definitions of syntax components referenced in this statement, see:

Semantics

The cast expression creates, if possible, new items of a given target type from the items of its input sequence. Specifically, a cast expression is evaluated as follows:

A cardinality check is performed first:

  1. if the quantifier of the target type is * the sequence may have any number of items,

  2. if the quantifier is + the input sequence must have at least one item,

  3. if the quantifier is ? the input sequence must have at most one item, and

  4. if there is no quantifier, the input sequence must have exactly one item.

If the cardinality of the input sequence does not match the quantifier of the target type, an error is raised. Then, each input item is cast to the target item type according to the following (recursive) rules.

Example 1 - Cast Expression

Select the last name of users who moved to their current address in 2015 or later.

SELECT u.lastName FROM Users u
WHERE
CAST (u.address.startDate AS Timestamp(0)) >=
CAST ("2015-01-01T00:00:00" AS Timestamp(0))

Since there is no literal for Timestamp values, to create such a value a string has to cast to a Timestamp type.