Skip to content
NEW BENCHMARKS

SurrealDB 3.x by the numbers

View

1/3

Course content preview

9: Casting and aliases

SurrealDB has a lot of data types, with names in lowercase such as bool, number, string, and array. Most types can be quickly changed into another by using a casting function. To cast into another type, put the target type inside <> angle brackets to the left.

SELECT 
name,
address,
-- 'floors' is a number, output as a string instead
<string> floors
FROM place;

One of the fields is called "<string> floors", because by default a field with a value set by an operation will have the operation as its name.

Response

[
{
"<string> floors": '8',
address: '2025 Statement Street, Riverdale',
name: 'Surreal Library'
}
]

If you find this field name ugly or want to rename it for another reason, you can use AS to give it a different name (give it an alias).

SELECT
name,
address,
<string>floors AS floors
FROM place;

Response

[
{
address: '2025 Statement Street, Riverdale',
floors: '8',
name: 'Surreal Library'
}
]

Casting will only work as long as the input is valid, mind you.

-- Works fine
<datetime>'1859-11-26';
-- Invalid input for datetime
<datetime>'EIGHTEEN FIFTY NINE, NOVEMBER TWENTY SIX';
-- A bool can only be 'true' or 'false'
<bool>"Hello there!";

Outputs

-------- Query 1 --------

d'1859-11-26T00:00:00Z'

-------- Query 2 --------

"Expected a datetime but cannot convert 'EIGHTEEN FIFTY NINE, NOVEMBER TWENTY SIX' into a datetime"

-------- Query 3 --------

"Expected a bool but cannot convert 'Hello there!' into a bool"

Casting also won't work for complex types because the database can't tell what you want to do. Take this object for example that you would like to turn into an array.

-- This cast won't work
<array>{
address: '2025 Statement Street, Riverdale',
floors: '8',
name: 'Surreal Library'
};

You might want an array holding each key, or arrays holding each key and value, and so on. In this case, take a look at SurrealDB's object functions to choose how to transform from one type into another. For example, you could use the object::values() function to just return the object's values.

-- Regular function syntax
object::values({
address: '2025 Statement Street, Riverdale',
floors: '8',
name: 'Surreal Library'
});

-- Method syntax: same function, same result
{
address: '2025 Statement Street, Riverdale',
floors: '8',
name: 'Surreal Library'
}.values();

Array of the object's values

[
'2025 Statement Street, Riverdale',
'8',
'Surreal Library'
]

Or you could use object::from_entries() to do the opposite: constructing an object from arrays of arrays.

object::from_entries([
[
'address',
'2025 Statement Street, Riverdale'
],
[
'floors',
'8'
],
[
'name',
'Surreal Library'
]
]);

Response

{
address: '2025 Statement Street, Riverdale',
floors: '8',
name: 'Surreal Library'
}

The array::map() function is another great way to turn one type into another. After typing .map(), choose a parameter name inside || for each item, and then choose what to do with each item before passing it on.

This example of the function takes two strings and turns each one of them into an object with three fields. The output is an array of objects created by each string.

["Surreal Library", "Riverdale"].map(|$name| {
name: $name,
is_cool: true,
name_is_long: $name.len() > 10
});

Response

[
{
is_cool: true,
name: 'Surreal Library',
name_is_long: true
},
{
is_cool: true,
name: 'Riverdale',
name_is_long: false
}
]
SurrealDB

The context layer for AI agents.

Documents, graphs, vectors, time-series, and memory - in one transaction, one query, one deployment.

Explore with AI

Independently verified

SOC 2 Type 2

GDPR

Cyber Essentials Plus

ISO 27001

Trust Centre

Copyright © 2026 SurrealDB Ltd. Registered in England and Wales. Company no. 13615201

Registered address: 3rd Floor 1 Ashley Road, Altrincham, Cheshire, WA14 2DT, United Kingdom

Trading address: Huckletree Oxford Circus, 213 Oxford Street, London, W1D 2LG, United Kingdom