cancel
Showing results for 
Search instead for 
Did you mean: 

Using variables in Cypher script

Michel
Node Clone

I need some  variables in Cypher script, see the use case:

City has three properties X,Y and Z
The value of Z should be calculated as Z = X + Y

Data and steps. 

1) The data, already in the database (here  only one node)
CREATE (a:City {DS: 'AAA'})

2) Adding the trhee properties  ( I could not find how to set the datatype, this one has worked)

MATCH (a:City) WHERE a.DS= 'AAA'
SET a.X = toFloat(0.0)
SET a.Y = toFloat(0.0)
SET a.Z = toFloat(0.0)

3) There is  a load csv, which sets the properties X and Y (it works fine)

MATCH (a:City) - setting the values X,Y
WHERE a.DS= 'AAA'
SET a.X = toFloat(2.5)
SET a.Y = toFloat(5.2)

4) The Z value is being calculated (it work as well)

MATCH (a:City) - setting the values X,Y
WHERE a.DS= 'AAA'
SET a.Z = toFloat(a.X) + toFloat(a.Y)

BTW: SET a.Z = a.X + a.Y  doesn't work as expected, it concatenates the values 😞

In the last step I need variables for toFloat(a.X) and toFloat(a.Y) , because I have a complicated expression with using several time both of them.

How to do it ?

Thanks,

 

Michel

 

3 ACCEPTED SOLUTIONS

ameyasoft
Graph Maven

Try this: Use SUM

SUM(toFloat(a.X) + toFloat(a.Y)) = 7.7

SET a.Z = SUM(toFloat(a.X) + toFloat(a.Y))

 

View solution in original post

ameyasoft
Graph Maven

In the last step I need variables for toFloat(a.X) and toFloat(a.Y) , because I have a complicated expression with using several time both of them.

WITH toFloat(a.X) as x1, toFloat(a.Y) as x2

SET a.Z = SUM(x1 + x2)

 

View solution in original post

glilienfield
Ninja
Ninja

Something is not correct. You should be able to add float numbers without wrapping in toFloat().  The only thing I can think of is that you imported the numbers in without using toFloat().  This is required, as all values are imported as strings, even things that represent numbers. This aligns with the behavior you observed that 'a.X + a.Y' concatenated the two values. This will happen if the two operands are strings. 

That being said, you just need to use 'with' statements to compute new values and pass to be used. Here is an example using your query. 

MATCH (a:City)
WHERE a.DS = 'AAA'
WITH a, toFloat(a.X) as x, toFloat(a.Y) as y
WITH a, x + y as z
SET a.z = z

 

View solution in original post

3 REPLIES 3

ameyasoft
Graph Maven

Try this: Use SUM

SUM(toFloat(a.X) + toFloat(a.Y)) = 7.7

SET a.Z = SUM(toFloat(a.X) + toFloat(a.Y))

 

ameyasoft
Graph Maven

In the last step I need variables for toFloat(a.X) and toFloat(a.Y) , because I have a complicated expression with using several time both of them.

WITH toFloat(a.X) as x1, toFloat(a.Y) as x2

SET a.Z = SUM(x1 + x2)

 

glilienfield
Ninja
Ninja

Something is not correct. You should be able to add float numbers without wrapping in toFloat().  The only thing I can think of is that you imported the numbers in without using toFloat().  This is required, as all values are imported as strings, even things that represent numbers. This aligns with the behavior you observed that 'a.X + a.Y' concatenated the two values. This will happen if the two operands are strings. 

That being said, you just need to use 'with' statements to compute new values and pass to be used. Here is an example using your query. 

MATCH (a:City)
WHERE a.DS = 'AAA'
WITH a, toFloat(a.X) as x, toFloat(a.Y) as y
WITH a, x + y as z
SET a.z = z