WITH p.name AS fullname, substr(fullname, 0, 5) AS nickname seems like it should work

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.

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.

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.

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.

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.

1 Like