11.2. Expressions

11.2.1. Expressions in general
11.2.2. Note on string literals
11.2.3. Case Expressions
11.2.4. Simple CASE
11.2.5. Generic CASE

11.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 nullable property: it’s a property, with a question mark or exclamation mark — n.prop?, rel.thisProperty!.
  • 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(*).
  • Relationship types: :REL_TYPE, :`REL TYPE`, :REL1|REL2.
  • 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

11.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

11.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.

11.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+ in +WHEN+ value +THEN+ result [+WHEN+ ...] [+ELSE+ default] +END+

Arguments:

  • in: A valid expression
  • value: An expression whose result will be compared to expression
  • result: This is the result expression used if the value expression matches the in 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

A string.

Result

result
5 rows

2

1

2

1

3


11.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+ result] +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

A string.

Result

result
5 rows

3

1

2

1

3