Column.cast
castvalue_typeon_problems
Group: Conversions
Documentation
Cast the column to a specific type.
In the Database backend, this will boil down to a CAST operation. In the in-memory backend, a conversion will be performed according to the following rules:
- Anything can be cast into the
Mixedtype. - Converting to a
Chartype, the elements of the column will be converted to text. If it is fixed length, the texts will be trimmed or padded on the right with the space character to match the desired length. - Conversion between numeric types will replace values exceeding the range
of the target type with
Nothing. - Converting decimal numbers into integers will truncate or round them,
depending on the backend. If more control is needed, use the various
rounding functions (such as
roundorfloor). - Booleans may also be converted to numbers, with
Truebeing converted to1andFalseto0. The reverse is not supported - useiifinstead. - A
Date_Timemay be converted into aDateorTimetype - the resulting value will be truncated to the desired type. - If a
Dateis to be converted toDate_Time, it will be set at midnight of the default system timezone. - For a
Mixedcolumn being converted into a specific type, each row is converted individually. If the target type cannot fit some of the values (for example due to too small range), aConversion_Failuremay be reported according to theon_problemsrules. The Database backends may fail withSQL_Errorinstead.
Arguments
value_type: TheValue_Typeto cast the column to.on_problems: Specifies how to handle problems if they occur, reporting them as warnings by default.
Remarks
Inexact Target Type
If the backend does not support the requested target type, the closest
supported type is chosen and a Inexact_Type_Coercion problem is
reported.
Casting Text values
The parse method should be used to convert text values into other types.
Due to this, a Mixed column containing values [2, "3"] will actually be
converted into [2, Nothing] when casting to Integer type.