Back to top
Documentation SurrealQL Data model Casting

Casting

In the SurrealDB type system, values can be converted to other values efficiently. This is useful if input is specified in a query which must be of a certain type, or if a user has provided a parameter with an incorrect type.

Type Description
<bool> Casts the subsequent value into a boolean
<int> Casts the subsequent value into a int
<float> Casts the subsequent value into a float
<string> Casts the subsequent value into a string
<number> Casts the subsequent value into a decimal
<decimal> Casts the subsequent value into a decimal
<datetime> Casts the subsequent value into a datetime
<duration> Casts the subsequent value into a duration

<bool>

The <bool> casting function converts a value into a boolean.

SELECT * FROM <bool> "true";
true
SELECT * FROM <bool> "some truthy value";
true
SELECT * FROM <bool> 1.375;
true
SELECT * FROM <bool> 0.00000;
false

<int>

The <int> casting function converts a value into an integer.

SELECT * FROM <int> 13.572948467293847293841093845679289;
13
SELECT * FROM <int> "13.572948467293847293841093845679289";
13
SELECT * FROM <int> "some truthy value";
0
SELECT * FROM <int> true;
1
SELECT * FROM <int> false;
0

<float>

The <float> casting function converts a value into a floating point number.

SELECT * FROM <float> 13.572948467293847293841093845679289;
13.572948467293847
SELECT * FROM <float> "13.572948467293847293841093845679289";
13.572948467293847
SELECT * FROM <float> "some truthy value";
0.0
SELECT * FROM <float> true;
1.0
SELECT * FROM <float> false;
0.0

<string>

The <string> casting function converts a value into a string.

SELECT * FROM <string> true;
"true"
SELECT * FROM <string> 1.3463;
"1.3463"
SELECT * FROM <string> false;
"false"

<number>

The <number> casting function converts a value into an infinite precision decimal number.

SELECT * FROM <number> 13.572948467293847293841093845679289;
"13.572948467293847293841093845679289"
SELECT * FROM <number> "13.572948467293847293841093845679289";
"13.572948467293847293841093845679289"
SELECT * FROM <number> "some truthy value";
"0"
SELECT * FROM <number> 1.193847193847193847193487E11;
"119384719384.7193847193487"
SELECT * FROM <number> true;
"1"

<decimal>

The <decimal> casting function converts a value into an infinite precision decimal number.

SELECT * FROM <decimal> 13.572948467293847293841093845679289;
"13.572948467293847293841093845679289"
SELECT * FROM <decimal> "13.572948467293847293841093845679289";
"13.572948467293847293841093845679289"
SELECT * FROM <decimal> "some truthy value";
"0"
SELECT * FROM <decimal> 1.193847193847193847193487E11;
"119384719384.7193847193487"
SELECT * FROM <decimal> true;
"1"

<datetime>

The <datetime> casting function converts a value into a datetime.

SELECT * FROM <datetime> "2022-06-07 will be parsed";
"2022-06-07"

<duration>

The <duration> casting function converts a value into a duration.

SELECT * FROM <duration> "1h30m will be parsed";
"1h30m"