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.

The following sections are organized by output value type for easier navigation. Here's how to find the conversion you're looking for:

  1. Locate the section for the output type: Each section represents an output type, where the title is the name of the type.
  2. Find the row for the input type: Within each section is a table where the first column represents the input type.
  3. 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 typeInput valueOutput value
Nullnullfalse
Integer0false
Integer1true
Decimal0.0false
Decimal1.0true
Float0.0false
Float1.0true
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 typeInput valueOutput value
Nullnull""
Booleantrue"true"
Booleanfalse"false"
Integer10"10"
Decimal10.5"10.5"
Float10.5"10.5"
Regex~/[a-z]+/"/[a-z]+/"
Date2024-10-18"2024-10-18"
Time12:00:00"12:00:00"
Date-time2024-10-18T12:00:00Z"2024-10-18T12:00Z"
Duration1 day"P1D"
URIhttps://croct.com"https://croct.com"

Number

The following table shows how different input types are converted to number values:

Input typeInput valueOutput typeOutput value
NullnullInteger0
BooleantrueInteger1
BooleanfalseInteger0
String"10"Integer10
String"1.5"Decimal1.5

Integer

The following table shows how different input types are converted to integer values:

Input typeInput valueOutput value
Nullnull0
Booleantrue1
Booleanfalse0
String""0
String"10"10
Decimal10.510
Float10.510

Decimal

The following table shows how different input types are converted to decimal values:

Input typeInput valueOutput value
Nullnull0.0
Booleantrue1.0
Booleanfalse0.0
String""0.0
String"10"10.0
Integer1010.0
Float10.510.5

Float

The following table shows how different input types are converted to float values:

Input typeInput valueOutput value
Nullnull0.0f
Booleantrue1.0f
Booleanfalse0.0f
String""0.0f
String"10"10.0f
Integer1010.0f
Decimal10.510.5f

Date

The following table shows how different input types are converted to date values:

Input typeInput valueOutput value
String"2024-10-18"2024-10-18
String"2024-W01-1"2024-01-01
String"2024-001"2024-01-01
String"2024-10-18T01:02:03"2024-10-18
Date-time2024-10-18T01:30:00Z2024-10-18

Time

The following table shows how different input types are converted to time values:

Input typeInput valueOutput value
String"01:02:03"01:02:03
String"01:02:03.123456"01:02:03.123456
String"01"01:00:00
Date-time2024-10-18T01:30:00Z01:30:00

Date-time

The following table shows how different input types are converted to date-time values:

Input typeInput valueOutput value
String"2024-10-18"2024-10-18T00:00:00
String"2024-10-18T01:02:03"2024-10-18T01:02:03
String"2024-10-18T01:02:03.123"2024-10-18T01:02:03.123
String"2024-10-18T01:02:03Z"2024-10-18T01:02:03
String"2024-10-18T01"2024-10-18T01:00:00
Date2024-10-182024-10-18T00:00:00

Regex

The following table shows how different input types are converted to regular expressions:

Input typeInput valueOutput value
String"abc"~/abc/
String"[a-z]+"~/\[a\-z\]\+/