Type conversion. Can be either:

  • Explicit via cast()
  • Implicit, in which case it will be automatically performed when required by the context.

Explicit conversion

Types can be converted from one to another using the cast() function.

Syntax

Flow chart showing the syntax of the CAST keyword

where:

  • expression can be a constant, a column, or an expression that evaluates to a value.
  • type refers to the desired data type.

cast can be used a part of arithmetic expression as normal

Examples

  1. SELECT
  2. cast(3L + 2L AS INT),
  3. cast(1578506142000000 AS TIMESTAMP),
  4. cast('10.2' AS DOUBLE),
  5. cast('行' AS INT);
castcast1cast2cast3
52020-01-08T17:55:42.000000Z10.234892

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 (float or double) to an integer number type (long, int, 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 char to a number type will return the corresponding unicode number and vice versa.

Precision loss examples

  1. SELECT
  2. cast(3.5 + 2 AS INT),
  3. cast(7234623 AS SHORT),
  4. cast(2334444.323 AS SHORT);
castcast1cast2
525663-24852

When casting numbers into a smaller data type, QuestDB will truncate the higher bits of this number.

Implicit conversion

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 is called implicit cast and does not require using the cast() function.

:::note

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.

Table showing the different possibilities the cast function supports, those are defined by an input and output types

:::note

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.

:::

  1. SELECT
  2. 1234L + 567,
  3. 1234L + 0.567,
  4. to_timestamp('2019-10-17T00:00:00', 'yyyy-MM-ddTHH:mm:ss') + 323,
  5. to_timestamp('2019-10-17T00:00:00', 'yyyy-MM-ddTHH:mm:ss') + 0.323;
columncolumn1column2column3
18011234.5672019-10-17T00:00:00.000323Z1571270400000000