8.2. Expressions

8.2.1. Expressions in general
8.2.2. Note on string literals
8.2.3. Case Expressions
8.2.4. Simple CASE
8.2.5. Generic CASE

8.2.1. Expressions in general

An expression in Cypher can be:

  • A numeric literal (integer or double): 13, 40000, 3.14.
  • A string literal: "Hello", 'World'.
  • A boolean literal: true, false, TRUE, FALSE.
  • An identifier: n, x, rel, myFancyIdentifier, `A name with weird stuff in it[]!`.
  • A property: n.prop, x.prop, rel.thisProperty, myFancyIdentifier.`(weird property name)`.
  • A parameter: {param}, {0}
  • A collection of expressions: ["a", "b"], [1,2,3], ["a", 2, n.property, {param}], [ ].
  • A function call: length(p), nodes(p).
  • An aggregate function: avg(x.prop), count(*).
  • A path-pattern: (a)-->()<--(b).
  • A predicate expression is an expression that returns true or false: a.prop = "Hello", length(p) > 10, has(a.name)
  • a CASE expression

8.2.2. Note on string literals

String literals can contain these escape sequences.

Escape sequenceCharacter

\t

Tab

\b

Backspace

\n

Newline

\r

Carriage return

\f

Form feed

\'

Single quote

\"

Double quote

\\

Backslash

8.2.3. Case Expressions

Cypher supports CASE expressions, which is a generic conditional expression, similar to if/else statements in other languages. Two variants of CASE exist — the simple form and the generic form.

8.2.4. 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:

  • expr: 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

8.2.5. Generic CASE

The predicates are evaluated in order until a true value is found, and the result value is used. If no match is found the expression in the ELSE clause is used, or null, if no ELSE case exists.

Syntax:

CASE
WHEN predicate THEN result
[WHEN ...]
[ELSE default]
END

Arguments:

  • predicate: A predicate that is tested to find a valid alternative.
  • result: This is the result expression used if the predicate matches.
  • default: The expression to use if no match is found.

Query. 

MATCH n
RETURN CASE
    WHEN n.eyes = 'blue'
THEN 1
WHEN n.age < 40
THEN 2
ELSE 3 END AS result

Result

result
5 rows

3

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 WHEN n.eyes = 'blue' THEN 1 WHEN n.age < 40 THEN 2 ELSE 3 END as result