Expressions in general
An expression in Cypher can be:
-
A decimal (integer or double) literal:
13
,-40000
,3.14
,6.022E23
. -
A hexadecimal integer literal (starting with
0x
):0x13zf
,0xFC3A9
,-0x66eff
. -
An octal integer literal (starting with
0
):01372
,02127
,-05671
. -
A string literal:
"Hello"
,'World'
. -
A boolean literal:
true
,false
,TRUE
,FALSE
. -
A variable:
n
,x
,rel
,myFancyVariable
,`A name with weird stuff in it[]!`
. -
A property:
n.prop
,x.prop
,rel.thisProperty
,myFancyVariable.`(weird property name)`
. -
A dynamic property:
n["prop"]
,rel[n.city + n.zip]
,map[coll[0]]
. -
A parameter:
{param}
,{0}
-
A list 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
and3 < 4
. -
A predicate expression is an expression that returns true or false:
a.prop = "Hello"
,length(p) > 10
,has(a.name)
. -
A regular expression:
a.name =~ "Tob.*"
-
A case-sensitive string matching expression:
a.surname STARTS WITH "Sven"
,a.surname ENDS WITH "son"
ora.surname CONTAINS "son"
-
A
CASE
expression.
Note on string literals
String literals can contain these escape sequences.
Escape sequence | Character |
---|---|
| Tab |
| Backspace |
| Newline |
| Carriage return |
| Form feed |
| Single quote |
| Double quote |
| Backslash |
| Unicode UTF-16 code point (4 hex digits must follow the |
| Unicode UTF-32 code point (8 hex digits must follow the |
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.
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 |
|
|
|
|
|
Try this query live create (_0:`bar`:`foo` {`age`:38, `eyes`:"brown", `name`:"Alice"}) create (_1 {`age`:25, `eyes`:"blue", `name`:"Bob"}) create (_2 {`age`:53, `eyes`:"green", `name`:"Charlie"}) create (_3 {`age`:54, `eyes`:"brown", `name`:"Daniel"}) create (_4:`Spouse` {`age`:41, `array`:["one", "two", "three"], `eyes`:"blue", `name`:"Eskil"}) create (_0)-[:`KNOWS`]->(_2) create (_0)-[:`KNOWS`]->(_1) create (_1)-[:`MARRIED`]->(_4) create (_1)-[:`KNOWS`]->(_3) create (_2)-[:`KNOWS`]->(_3) ; match (n) return CASE n.eyes WHEN 'blue' THEN 1 WHEN 'brown' THEN 2 ELSE 3 END as result
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 |
|
|
|
|
|
Try this query live create (_0:`bar`:`foo` {`age`:38, `eyes`:"brown", `name`:"Alice"}) create (_1 {`age`:25, `eyes`:"blue", `name`:"Bob"}) create (_2 {`age`:53, `eyes`:"green", `name`:"Charlie"}) create (_3 {`age`:54, `eyes`:"brown", `name`:"Daniel"}) create (_4:`Spouse` {`age`:41, `array`:["one", "two", "three"], `eyes`:"blue", `name`:"Eskil"}) create (_0)-[:`KNOWS`]->(_2) create (_0)-[:`KNOWS`]->(_1) create (_1)-[:`MARRIED`]->(_4) create (_1)-[:`KNOWS`]->(_3) create (_2)-[:`KNOWS`]->(_3) ; match (n) return CASE WHEN n.eyes = 'blue' THEN 1 WHEN n.age < 40 THEN 2 ELSE 3 END as result