Neo4j Cypher - String to Integer

neo4j concatenate strings
neo4j tointeger
neo4j collect
neo4j data types
neo4j timestamp
neo4j distinct
neo4j cypher tail
neo4j size vs count

Here the value in property r.weight is a string. i have tried following possibilities to convert it into a integer,


but nothing works. Is there a proper function to do so?

reduce(total = 0, r in relationships(p): total + INT(r.weight))

Note : Its not a possible duplicate of Gremlin type conversion

Version : 1.9.5

As stated in the comments above there is no easy way to do this with Cypher in 1.9.x. As a workaround you could use the neo4j-shell and use the gsh or jsh or eval command to execute a script that iterates over all relationships and converts the value of weight property from String to numeric.

9.1. Conversion Functions - Chapter 9. Data Structures, tries its best to convert the value to a list of integers. apoc.convert.toStringList(​value). tries its best to convert the value to a list of strings. apoc.convert. When toString() is applied to a temporal value, it returns a string representation suitable for parsing by the corresponding temporal functions. This string will therefore be formatted according to the ISO 8601 format.

In Cypher 3.3.5 toInt() is deprecated, rather use toInteger()

i.e. toInteger(r.weight) - this works fine

Chapter 4. Functions, This section contains information on all functions in the Cypher query language. Converts an integer or string value to a floating point number. toInteger(). Returns a string in which the order of all characters in the original string have been reversed. right() String. Returns a string containing the specified number of rightmost characters of the original string. round() Numeric. Returns the value of a number rounded to the nearest integer. rTrim() String

To extend chamber5's answer.

Presumably you are interested in doing some arithmetic with the weights. An obvious use case is to sum the weights going into a node. For example, in a social network perhaps we are interested in how many messages Ada has received from other nodes (colleagues); with one message increasing the weight of that edge or relationship by one.

Thus we want to MATCH for all relationships of a given type (r) going into a Person node with the name property of "Ada".

MATCH ()-[r]->(n:Person {name:"Ada"})
RETURN sum(toInteger(r.weight)) AS total_weights_in;

Of course we could look for bidirectional total weights also by adjusting our ASCii art:

MATCH ()<-[r]->(n:Person {name:"Ada"})
RETURN sum(toInteger(r.weight)) AS total_weights_in;

This work in Neo4j version 3.4.9.

4.2. Scalar functions - Chapter 4. Functions, size() returns the number of Unicode characters in a string. Syntax: size(string). Returns: An Integer. Byte arrays are not considered a first class data type by Cypher, so do not have a literal representation. Sorting of special characters Strings that contain characters that do not belong to the Basic Multilingual Plane ( BMP ) can have inconsistent or non-deterministic ordering in Neo4j.

4.8. String functions - Chapter 4. Functions, left(). left() returns a string containing the specified number of leftmost characters of the original string. Syntax:  The length() and size() functions are quite similar, and so it is important to take note of the difference.length() only works for paths, while size() only works for the three types: strings, lists and pattern expressions.

2.1. Values and types - Chapter 2. Syntax, Cypher provides first class support for a number of data types. Number, an abstract type, which has the subtypes Integer and Float; String; Boolean; The  Selecting a DateTime as the datetime component and overwriting the time zone will adjust the local time to keep the same point in time. Selecting a DateTime or Time as the time component and overwriting the time zone will adjust the local time to keep the same point in time.

11.3. Number Format Conversions - Chapter 11. Mathematical, apoc.number.format(number) format a long or double using the default system pattern and language to produce a string; apoc.number.format(number, pattern)  Currently neo4j is somewhere in the middle which causes headaches if I want to alter data from Cypher and that data is read & written by an external program coded to use int properties. This comment has been minimized.