Skip to content

Type Conversion/Type coercions

Converting an expression of a given type to another type is known as type conversion.

Type coercions functions

Function Description
toBoolean() Converts a string value to a boolean value.
toFloat() Converts an integer or string value to a floating point number.
toInteger() Converts a floating point or string value to an integer value.
toString() Converts non-compound types of data, such as numbers, booleans, and so on, to strings.
toSet() Converts a list or set value to a set value.
type() Returns the string representation of the relationship type.

Examples

nebula> UNWIND [true, false, 'true', 'false', NULL] AS b \
        RETURN toBoolean(b) AS b;
+----------+
| b        |
+----------+
| true     |
| false    |
| true     |
| false    |
| __NULL__ |
+----------+

nebula> RETURN toFloat(1), toFloat('1.3'), toFloat('1e3'), toFloat('not a number');
+------------+----------------+----------------+-------------------------+
| toFloat(1) | toFloat("1.3") | toFloat("1e3") | toFloat("not a number") |
+------------+----------------+----------------+-------------------------+
| 1.0        | 1.3            | 1000.0         | __NULL__                |
+------------+----------------+----------------+-------------------------+

nebula> RETURN toInteger(1), toInteger('1'), toInteger('1e3'), toInteger('not a number');
+--------------+----------------+------------------+---------------------------+
| toInteger(1) | toInteger("1") | toInteger("1e3") | toInteger("not a number") |
+--------------+----------------+------------------+---------------------------+
| 1            | 1              | 1000             | __NULL__                  |
+--------------+----------------+------------------+---------------------------+

nebula> MATCH (a:player)-[e]-() \
        RETURN type(e);
+----------+
| type(e)  |
+----------+
| "follow" |
| "follow" |
+----------+

nebula> MATCH (a:player {name: "Tim Duncan"}) \
        WHERE toInteger(right(id(a),3)) == 100 \
        RETURN a;
+----------------------------------------------------+
| a                                                  |
+----------------------------------------------------+
| ("player100" :player{age: 42, name: "Tim Duncan"}) |
+----------------------------------------------------+

nebula> MATCH (n:player) \
        WITH n LIMIT toInteger(ceil(1.8)) \
        RETURN count(*) AS count;
+-------+
| count |
+-------+
| 2     |
+-------+

nebula> RETURN toString(9669) AS int2str, toString(null) AS null2str;
+---------+----------+
| int2str | null2str |
+---------+----------+
| "9669"  | __NULL__ |
+---------+----------+

nebula> RETURN toSet(list[1,2,3,1,2]) AS list2set;
+-----------+
| list2set  |
+-----------+
| {3, 1, 2} |
+-----------+

nebula> RETURN toSet(set{1,2,3,1,2}) AS set2set;
+-----------+
| set2set   |
+-----------+
| {3, 2, 1} |
+-----------+

Last update: May 26, 2022