String concatenation operators
Cypher® contains two operators for the concatenation of STRING
values:
-
||
-
+
The two operators are functionally equivalent.
However, ||
is GQL conformant, while +
is not.
For additional expressions that evaluate to STRING
values, see String functions.
Examples
||
and +
RETURN 'Neo' || '4j' AS result1,
'Neo' + '4j' AS result2
result1 | result2 |
---|---|
|
|
Rows: 1 |
The toString()
function can be used to concatenate non-STRING
values into a STRING
value.
toString()
functionRETURN 'The number is: ' || toString(42) AS result
result |
---|
|
Rows: 1 |
Cypher does not insert spaces when concatenating STRING
values.
STRING
concatenationRETURN 'Alpha' || 'Beta' AS result1,
'Alpha' || ' ' || 'Beta' AS result2
result1 | result2 |
---|---|
|
|
Rows: 1 |
STRING
propertiesCREATE (p:Person {firstName: 'Keanu', lastName: 'Reeves'})
SET p.fullName = p.firstName || ' ' || p.lastName
RETURN p.fullName AS fullName
fullName |
---|
|
Rows: 1 |
STRING
concatenationRETURN 'Hello' || ', ' || 'World' AS result
result |
---|
|
Rows: 1 |
STRING
concatenationRETURN 'My favorite fruits are: ' || 'apples' || ', ' || 'bananas' || ', and' || 'oranges' || '.' AS result
result |
---|
|
Rows: 1 |
String concatenation, LIST
values, and NULL
STRING
values in a LIST
can be concatenated using the reduce()
function.
STRING
values in a LIST
WITH ['Neo', '4j'] AS list
RETURN reduce(acc = '', item IN list| acc || item) AS result
result |
---|
|
Rows: 1 |
The following query uses the head()
function to start the accumulator with the first item
in the list
, while the tail()
function returns the remaining items in the list
.
The reduce()
function then concatenates these items with commas.
,
) in a STRING
concatenated from STRING
values in a LIST
WITH ['Apples', 'Bananas', 'Oranges'] AS list
RETURN 'My favorite fruits are: ' || reduce(acc = head(list), item IN tail(list) | acc || ', ' || item) || '.' AS result
result |
---|
|
Rows: 1 |
Concatenating a STRING
value with NULL
returns NULL
.
To skip the first NULL
value in a list of expressions, use the coalesce()
function.
In the following query, coalesce()
is used with reduce()
to replace each NULL
value in the LIST
with an empty STRING
(''
).
This ensures that all NULL
values are effectively skipped, allowing the reduce()
function to concatenate the remaining STRING
values.
reduce()
and coalesce()
to skip NULL
values when concatenating a LIST
WITH ['Apples', NULL, 'Bananas', NULL, 'Oranges', NULL] AS list
RETURN 'My favorite fruits are: ' || reduce(acc = head(list), item IN tail(list) | acc || coalesce(', ' || item, '')) || '.' AS result
result |
---|
|
Rows: 1 |
If a LIST
is empty, reduce()
will return NULL
because there are no elements to process.
In such cases, coalesce()
can be used to replace the NULL
with a default value (e.g., 'none'
).
reduce()
and coalesce()
to handle empty LIST
valuesUNWIND [['Apples', 'Bananas', 'Oranges'], ['Pears'], []] AS list
RETURN 'My favorite fruits are: ' || coalesce(reduce(acc = head(list), item IN tail(list) | acc || ', ' || item), 'none') || '.' AS result
result |
---|
|
|
|
Rows: 3 |
Additionally, list comprehension allows concatenating a STRING
value to each item in a LIST
to generate a new LIST
of modified STRING
values.
STRING
concatenation on LIST
itemsWITH ['Apples', 'Bananas', 'Oranges'] AS list
RETURN [item IN list | 'Eat more ' || item || '!'] AS result
result |
---|
|
Rows: 1 |