cancel
Showing results for 
Search instead for 
Did you mean: 

Join the community at Nodes 2022, our free virtual event on November 16 - 17.

clem
Graph Steward

In SQL you can use variables previously defined with AS in subsequent clause, but Cypher doesn't seem to support that.

in this (contrived) example, this doesn't work. The substr() won't accept fullname.

WITH p.name AS fullname, substr(fullname, 0, 5) AS nickname

The work around is to use two WITH statements (which is a bit uglier):

WITH p.name AS fullname
WITH fullname, substr(fullname, 0, 5) AS nickname

It would be nice if Cypher could support this.

4 Comments
tard_gabriel
Ninja
Ninja

WITH always refer to the last query part, in this case, I can't see it but I would guess this solution would work.

WITH p.name AS fullname, substr(p.name, 0 ,5) AS nickname will do it.

clem
Graph Steward

I'm just suggesting this as it makes transference of SQL skills to Cypher a tiny bit easier.

Your way is definitely another work around.

mark_needham
Neo4j
Neo4j

It might be worth posting this as an issue/suggestion on the neo4j GitHub repository - Issues · neo4j/neo4j · GitHub

At least so it's written down, although I'm not saying that it will be worked on as a high priority as I think the general way that WITH works is that you can only reference any variables that were in scope before that point.

mdfrenchman
Graph Voyager

I think the pattern of WITH p.name AS fullname, substr(fullname, 0, 5) AS nickname can get extremely messy and hard to follow. With the current implementation of two WITHs, the rule that the variable has to appear in the previous WITH makes it much easier to track variable names and bugs.