Type conversion. Can be either:
- Explicit via
- Implicit, in which case it will be automatically performed when required by the context.
Types can be converted from one to another using the
expressioncan be a constant, a column, or an expression that evaluates to a value.
typerefers to the desired data type.
cast can be used a part of arithmetic expression as normal
Explicit casting of an expression to a smaller data type may result in loss of data when the output data type is smaller than the expression.
- Casting a decimal number type (
double) to an integer number type (
short) will result in decimals drop.
- If the integer part being cast is larger than the resulting data type, it will be resized by truncating bits.
- Conversions from
charto a number type will return the corresponding
unicodenumber and vice versa.
When casting numbers into a smaller data type, QuestDB will truncate the higher bits of this number.
Type casting may be necessary in certain context such as
- Operations involving various different types
- Inserting values where the originating type is different from the destination column type.
QuestDB will attempt to convert to the data type required by the context. This
implicit cast and does not require using the
QuestDB will only perform implicit cast when they would not result in data being truncated or precision being lost.
The below chart illustrates the explicit and implicit cast available in QuestDB.
Implicit casting prevents data loss. When an operation involves multiple types, the resulting type will be the smallest possible type so that no data is lost.
When inserting into a table, QuestDB will cast data implicitly to match the type of the destination column.
The above insert would have been equivalent to running with explicit cast, but QuestDB took care of this step automatically.