cancel
Showing results for 
Search instead for 
Did you mean: 

Head's Up! These forums are read-only. All users and content have migrated. Please join us at community.neo4j.com.

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

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

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