8.3. Simple CASE

The expression is calculated, and compared in order with the WHEN clauses until a match is found. If no match is found the expression in the ELSE clause is used, or null, if no ELSE case exists.

Syntax:

CASE test
WHEN value THEN result
[WHEN ...]
[ELSE default]
END

Arguments:

  • test: A valid expression.
  • value: An expression whose result will be compared to the test expression.
  • result: This is the result expression used if the value expression matches the test expression.
  • default: The expression to use if no match is found.

Query. 

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

Result

result
5 rows

2

1

2

1

3

Try this query live. create (_0 {`age`:54, `eyes`:"brown", `name`:"Daniel"}) create (_1:`Spouse` {`age`:41, `array`:["one", "two", "three"], `eyes`:"blue", `name`:"Eskil"}) create (_2:`foo`:`bar` {`age`:38, `eyes`:"brown", `name`:"Alice"}) create (_3 {`age`:25, `eyes`:"blue", `name`:"Bob"}) create (_4 {`age`:53, `eyes`:"green", `name`:"Charlie"}) create _2-[:`KNOWS`]->_3 create _2-[:`KNOWS`]->_4 create _3-[:`KNOWS`]->_0 create _3-[:`MARRIED`]->_1 create _4-[:`KNOWS`]->_0 match n return CASE n.eyes WHEN 'blue' THEN 1 WHEN 'brown' THEN 2 ELSE 3 END as result