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 FROMplace;
One of the fields is called "<string> floors", because the operation itself becomes the name of a field when an operation is used to set it.
Casting will only work as long as the input is valid, of course.
-- 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 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.
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.