Prev |  | Next

## 8.2. Expressions

### 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)`.
- An operator application: `1 + 2` and `3 < 4`.
- 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 sequence | Character
--- | ---
`\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`

### 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`

Copyright © 2014 Neo Technology

Prev | Up | Next
| Home |