CASE WHEN vs. apoc.do.case()

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:

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
1 Like

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?

1 Like

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

1 Like