cancel
Showing results for 
Search instead for 
Did you mean: 

CASE WHEN vs. apoc.do.case()

clem
Graph Steward

I'm puzzled about CASE WHEN.

This works (I added the parens the expression that RETURN returns to clarify).

MATCH (n)

RETURN
(CASE n.eyes
WHEN 'blue'
THEN 1
WHEN 'brown'
THEN 2
ELSE 3 END) AS result

but this doesn't work (syntax error). I'm not clear why. CASE is not expected to follow MATCH (but CALL is)

MATCH (n)

CASE n.eyes
WHEN 'blue'
THEN 1
WHEN 'brown'
THEN 2
ELSE 3 END as result

RETURN result

It looks like I should use apoc.do.case() instead, which is described here:

The documentation on this stuff is a bit lacking and not easy to understand:

1 ACCEPTED SOLUTION

dana_canzano
Neo4j
Neo4j

to avoid the syntax error of

MATCH (n)

CASE n.eyes
WHEN 'blue'
THEN 1
WHEN 'brown'
THEN 2
ELSE 3 END as result

RETURN result

rewrite this as

MATCH (n)
with n,
CASE n.eyes
WHEN 'blue'
THEN 1
WHEN 'brown'
THEN 2
ELSE 3 END as result
RETURN result

View solution in original post

3 REPLIES 3

dana_canzano
Neo4j
Neo4j

to avoid the syntax error of

MATCH (n)

CASE n.eyes
WHEN 'blue'
THEN 1
WHEN 'brown'
THEN 2
ELSE 3 END as result

RETURN result

rewrite this as

MATCH (n)
with n,
CASE n.eyes
WHEN 'blue'
THEN 1
WHEN 'brown'
THEN 2
ELSE 3 END as result
RETURN result

Thanks! The documentation could be improved on this. I'm tempted to redo the documentation and make a Pull Request so that the documentation uses the Movie DB so that a user can more easily try it out.

Would that be OK?

ill report to the documentation team. i can see where improvements would help since my response was not the 1 second response but rather took some fiddling to get it right...a minute or 2

Nodes 2022
Nodes
NODES 2022, Neo4j Online Education Summit

On November 16 and 17 for 24 hours across all timezones, you’ll learn about best practices for beginners and experts alike.