Skip to content
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.

Output
[
  {
    "<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;


Output
[
  {
    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'
]
]);


Output
{
  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
});


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


THE PLATFORM

Everything an application and its agents know. Five surfaces, one engine.

IN PRODUCTION

Trusted at scale. Samsung, Nvidia, Verizon, Tencent and Walmart run on SurrealDB.

14,000+

Developers building on SurrealDB Cloud

4M+

Developers building on SurrealDB worldwide

FROM THE TEAMS

SurrealDB gives us a foundation where we can unify semantic search, knowledge graphs, and AI-driven decision making without stitching together multiple systems. Collapsing responsibility into SurrealDB has become our default engineering posture.
Justin Foley

VP of Engineering, Later

SurrealDB

The context and memory layer for AI agents

Database. Graphs, vectors, documents and relational data in one engine, in a single ACID transaction.
Agent Memory. Connects and retrieves context wherever your data lives, every fact carrying its source.
Cloud. Fully managed, in the cloud provider and region you choose.

Explore with AI

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