An expression in Cypher can be:
13
, 40000
, 3.14
.
"Hello"
, 'World'
.
true
, false
, TRUE
, FALSE
.
n
, x
, rel
, myFancyIdentifier
, `A name with weird stuff in it[]!`
.
n.prop
, x.prop
, rel.thisProperty
, myFancyIdentifier.`(weird property name)`
.
{param}
, {0}
["a", "b"]
, [1,2,3]
, ["a", 2, n.property, {param}]
, [ ]
.
length(p)
, nodes(p)
.
avg(x.prop)
, count(*)
.
(a)-->()<--(b)
.
a.prop = "Hello"
, length(p) > 10
, has(a.name)
CASE
expression
String literals can contain these escape sequences.
Escape sequence | Character |
---|---|
| Tab |
| Backspace |
| Newline |
| Carriage return |
| Form feed |
| Single quote |
| Double quote |
| Backslash |
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.
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
expression.
test
expression.
Query.
MATCH n RETURN CASE n.eyes WHEN 'blue' THEN 1 WHEN 'brown' THEN 2 ELSE 3 END AS result
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
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:
Query.
MATCH n RETURN CASE WHEN n.eyes = 'blue' THEN 1 WHEN n.age < 40 THEN 2 ELSE 3 END AS result
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
Copyright © 2013 Neo Technology