Skip to main content
Version: 1.x

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 may have provided a parameter with an incorrect type.

TypeDescription
<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
<array<T>>Casts the subsequent value into an array of T
<record>Casts the subsequent value into a record
<uuid>Casts the subsequent value into a UUID

<bool>

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

SELECT * FROM <bool> "true";

true
SELECT * FROM <bool> "false";

false

<int>

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

SELECT * FROM <int> 53;

53

<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

<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> 1.193847193847193847193487E11;

"119384719384.7193847193487"

<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> 1.193847193847193847193487E11;

"119384719384.7193847193487"

<datetime>

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

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

"2022-06-07T00:00:00Z"

<duration>

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

SELECT * FROM <duration> "1h30m";

"1h30m"

<array<T>>

The <array<T>> casting function converts a value into an array of T.

When using this casting function, the value must be an array and each element in the array will be cast to T.

SELECT * FROM <array<int>> ["42", "314", "271", "137", "141"];

[42, 314, 271, 137, 141]
SELECT * FROM <array<string>> [42, 314, 271, 137, 141];

["42", "314", "271", "137", "141"]

<record>

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

Keep in mind when using this casting function that if the equivalent record id does not exist, it will not return anything.

SELECT id FROM <record> (s"person:hrebrffwm4sr2yifglta");;

{ id: person:hrebrffwm4sr2yifglta }


<uuid>

The <uuid> casting function converts a value into a UUID.

Keep in mind when using this casting function that if the equivalent record id does not exist, it will not return anything.

SELECT id FROM <uuid> "a8f30d8b-db67-47ec-8b38-ef703e05ad1b";

[ u'a8f30d8b-db67-47ec-8b38-ef703e05ad1b' ]