Casting data values
Cypher® supports a number of functions to cast values to different data types. This section will provide an overview of those functions, as well examples of how to use them in practice.
Functions for converting data values
The following functions are available for casting data values:
Function | Description |
---|---|
|
Converts a string, integer, or boolean value to a boolean value. |
|
Converts a list of values and returns a list of boolean values.
If any values are not convertible to boolean they will be |
|
Converts a string, integer or boolean value to a boolean value.
For any other input value, |
|
Converts an integer, floating point, or a string value to a floating point number value.
Otherwise |
|
Converts a list of values and returns a list of floating point values.
If any values are not convertible to floating point they will be |
|
Converts an integer, floating point, or a string value to a floating point number.
For any other input value, |
|
Converts a boolean, integer, floating point or a string value to an integer value. |
|
Converts a list of values and returns a list of integer values. If any values are not convertible to integer they will be |
|
Converts a boolean, integer, floating point or a string value to an integer value.
For any other input value, |
|
Converts an integer, float, boolean, string, point, duration, date, time, localtime, localdatetime, or datetime value to a string value. |
|
Converts a list of values and returns a list of string values.
If any values are not convertible to string they will be |
|
Converts an integer, float, boolean, string, point, duration, date, time, localtime, localdatetime, or datetime value to a string.
For any other input value, |
More information about these, and many other functions, can be found in the section on Functions.
Examples
The following graph is used for the examples below:
To recreate it, run the following query against an empty Neo4j database:
CREATE (keanu:Person {name:'Keanu Reeves', age: 58, active:true}),
(carrieAnne:Person {name:'Carrie-Anne Moss', age: 55, active:true}),
(keanu)-[r:KNOWS {since:1999}]->(carrieAnne)
Returning converted values
In the below query, the function toFloat
is used to cast two string values.
It shows that null
is returned if the data casting is not possible.
MATCH (keanu:Person {name:'Keanu Reeves'})
RETURN toFloat(keanu.age), toInteger(keanu.name)
toFloat(keanu.age) | toInteger(keanu.name) |
---|---|
|
|
If the function toFloat
is passed an unsupported value (such as a Date
), it will throw an error:
WITH date({
year: 2023, month: 5, day: 2
}) AS d
RETURN toFloat(d)
Type mismatch: expected Float, Integer, Number or String but was Date (line 4, column 16 (offset: 66))
"RETURN toFloat(d)"
However, if the same value is passed to the function toFloatOrNull
, null
will be returned.
WITH date({
year: 2023, month: 5, day: 2
}) AS d
RETURN toFloatOrNull(d)
toFloatOrNull(d) |
---|
|
It is also possible to return casted values as a list.
The below query uses the toStringList
to cast all passed values into strings, and return them in a list:
MATCH (keanu:Person {name:'Keanu Reeves'})
RETURN toStringList([keanu.name, keanu.age]) AS keanuList
keanuList |
---|
|
Updating property value types
The functions to cast data values can be used to update property values on nodes and relationships.
The below query casts the age
(integer), active
(boolean), and since
(integer) properties to string values:
MATCH (keanu:Person {name:'Keanu Reeves'})-[r:KNOWS]-()
SET keanu.age = toString(keanu.age),
keanu.active = toString(keanu.active),
r.since = toString(r.since)
RETURN keanu.age, keanu.active, r.since
keanu.age | keanu.active | r.since |
---|---|---|
|
|
|
Was this page helpful?