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
Result
result1 result2

"Neo4j"

"Neo4j"

Rows: 1

The toString() function can be used to concatenate non-STRING values into a STRING value.

Concatenation using toString() function
RETURN 'The number is: ' || toString(42) AS result
Result
result

"The number is: 42"

Rows: 1

Cypher does not insert spaces when concatenating STRING values.

Adding whitespaces in STRING concatenation
RETURN 'Alpha' || 'Beta' AS result1,
       'Alpha' || ' ' || 'Beta' AS result2
Result
result1 result2

"AlphaBeta"

"Alpha Beta"

Rows: 1

Concatenating STRING properties
CREATE (p:Person {firstName: 'Keanu', lastName: 'Reeves'})
SET p.fullName = p.firstName || ' ' || p.lastName
RETURN p.fullName AS fullName
Result
fullName

"Keanu Reeves"

Rows: 1

Adding separators in STRING concatenation
RETURN 'Hello' || ', ' || 'World' AS result
Result
result

"Hello, World"

Rows: 1

Adding prefix, suffix, and separators in STRING concatenation
RETURN 'My favorite fruits are: ' || 'apples' || ', ' || 'bananas' || ', and' || 'oranges' || '.' AS result
Result
result

"My favorite fruits are: apples, bananas, and oranges."

Rows: 1

String concatenation, LIST values, and NULL

STRING values in a LIST can be concatenated using the reduce() function.

Concatenate STRING values in a LIST
WITH ['Neo', '4j'] AS list
RETURN reduce(acc = '', item IN list| acc || item) AS result
Result
result

"Neo4j"

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.

Add prefix and a separator (,) 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
result

"My favorite fruits are: Apples, Bananas, Oranges."

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.

Using 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
result

"My favorite fruits are: Apples, Bananas, Oranges."

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').

Using reduce() and coalesce() to handle empty LIST values
UNWIND [['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
result

"My favorite fruits are: Apples, Bananas, Oranges."

"My favorite fruits are: Pears."

"My favorite fruits are: none."

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.

List comprehension with STRING concatenation on LIST items
WITH ['Apples', 'Bananas', 'Oranges'] AS list
RETURN [item IN list | 'Eat more ' || item || '!'] AS result
Result
result

["Eat more Apples!", "Eat more Bananas!", "Eat more Oranges!"]

Rows: 1