This website uses cookies. By clicking Accept, you consent to the use of cookies. Click Here to learn more about how we use cookies.

Turn on suggestions

Auto-suggest helps you quickly narrow down your search results by suggesting possible matches as you type.

Showing results for

- Neo4j
- Community Corner
- General Discussions
- Using variables in Cypher script

Options

- Subscribe to RSS Feed
- Mark Topic as New
- Mark Topic as Read
- Float this Topic for Current User
- Bookmark
- Subscribe
- Mute
- Printer Friendly Page

Options

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content

08-24-2022 09:07 AM - edited 08-24-2022 09:16 AM

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

Solved! Go to Solution.

3 ACCEPTED SOLUTIONS

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content

08-24-2022 12:22 PM

Try this: Use SUM

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

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

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content

08-24-2022 01:02 PM

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)

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content

08-24-2022 01:52 PM

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
```

3 REPLIES 3

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content

08-24-2022 12:22 PM

Try this: Use SUM

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

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

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content

08-24-2022 01:02 PM

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)

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content

08-24-2022 01:52 PM

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
```

Related Content

- Discover Neo4j AuraDB Free — Week 24 — NYTimes Article Knowledge Graph in General Discussions
- Toolbelt Trifecta: Connecting to Neo4j with Java and AWS Lambda in General Discussions
- Discover AuraDB Free: Importing GEDCOM Files and Exploring Genealogy/Ancestry Data as a Graph in General Discussions
- Updated: Efficient Neo4j Data Import Using Cypher-Scripts in General Discussions