类型转换¶
类型转换是指将表达式的类型转换为另一个类型。
类型强制转换函数¶
函数 | 说明 |
---|---|
toBoolean() | 将字符串转换为布尔。 |
toFloat() | 将整数或字符串转换为浮点数。 |
toInteger() | 将浮点或字符串转换为整数。 |
toString() | 将非复合类型的数据,例如数字、布尔值等,转换为字符串。 |
toSet() | 将列表或集合转换为集合。 |
type() | 返回字符串格式的关系类型。 |
示例¶
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} |
+-----------+
最后更新:
March 7, 2023