Conversions
Understand type conversions in CQL.
Type conversion refers to the process of changing a value from one type to another. This is what allows you to specify values without worrying about their type.
Conversion can be performed in two ways: explicitly or implicitly. Explicit conversion involves using the cast modifier, while implicit conversion happens automatically when you use a value in places that expect a different type.
The conversion rules focus on predictability, ensuring that anything unusual is reported as an error. This is to avoid scenarios where an expression works but does not do what you expect.
If a conversion is not explicitly mentioned in the following sections, it means that it is not supported and will result in an error.
The following sections are organized by output value type for easier navigation. Here's how to find the conversion you're looking for:
- Locate the section for the output type: Each section represents an output type, where the title is the name of the type.
- Find the row for the input type: Within each section is a table where the first column represents the input type.
- Explore the examples of input values: Multiple columns for the same input type represent different input values.
For example, if you want to convert from string to integer, locate the section titled "Integer" and the row with the input type "String".
Boolean
The following table shows how different input types are converted to boolean values:
Input type | Input value | Output value |
---|---|---|
Null | null | false |
Integer | 0 | false |
Integer | 1 | true |
Decimal | 0.0 | false |
Decimal | 1.0 | true |
Float | 0.0 | false |
Float | 1.0 | true |
String | "" | false |
String | "true" | true |
String | "false" | false |
String | "1" | true |
String | "0" | false |
String
The following table shows how different input types are converted to string values:
Input type | Input value | Output value | |
---|---|---|---|
Null | null | "" | |
Boolean | true | "true" | |
Boolean | false | "false" | |
Integer | 10 | "10" | |
Decimal | 10.5 | "10.5" | |
Float | 10.5 | "10.5" | |
Regex | ~/[a-z]+/ | "/[a-z]+/" | |
Date | 2024-10-18 | "2024-10-18" | |
Time | 12:00:00 | "12:00:00" | |
Date-time | 2024-10-18T12:00:00Z | "2024-10-18T12:00Z" | |
Duration | 1 day | "P1D" | |
URI | https://croct.com | "https://croct.com" |
Number
The following table shows how different input types are converted to number values:
Input type | Input value | Output type | Output value |
---|---|---|---|
Null | null | Integer | 0 |
Boolean | true | Integer | 1 |
Boolean | false | Integer | 0 |
String | "10" | Integer | 10 |
String | "1.5" | Decimal | 1.5 |
Integer
The following table shows how different input types are converted to integer values:
Input type | Input value | Output value |
---|---|---|
Null | null | 0 |
Boolean | true | 1 |
Boolean | false | 0 |
String | "" | 0 |
String | "10" | 10 |
Decimal | 10.5 | 10 |
Float | 10.5 | 10 |
Decimal
The following table shows how different input types are converted to decimal values:
Input type | Input value | Output value |
---|---|---|
Null | null | 0.0 |
Boolean | true | 1.0 |
Boolean | false | 0.0 |
String | "" | 0.0 |
String | "10" | 10.0 |
Integer | 10 | 10.0 |
Float | 10.5 | 10.5 |
Float
The following table shows how different input types are converted to float values:
Input type | Input value | Output value |
---|---|---|
Null | null | 0.0f |
Boolean | true | 1.0f |
Boolean | false | 0.0f |
String | "" | 0.0f |
String | "10" | 10.0f |
Integer | 10 | 10.0f |
Decimal | 10.5 | 10.5f |
Date
The following table shows how different input types are converted to date values:
Time
The following table shows how different input types are converted to time values:
Date-time
The following table shows how different input types are converted to date-time values:
Regex
The following table shows how different input types are converted to regular expressions: