Skip to content

Commit 4d0479f

Browse files
third
1 parent 5e3a923 commit 4d0479f

File tree

1 file changed

+102
-8
lines changed

1 file changed

+102
-8
lines changed
Lines changed: 102 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,35 +1,129 @@
1-
= String operators
1+
= String concatenation operators
22
:description: Information about Cypher's string operators.
33
:table-caption!:
44

5-
Cypher contains two functionally equivalent operators for the concatenation of `STRING` values:
5+
Cypher contains two operators for the concatenation of `STRING` values:
66

7-
* `+`
87
* `||`
8+
* `+`
99
10-
`||` is xref:appendix/gql-conformance/index.adoc[GQL conformant], `+` is not.
10+
The two operators are functionally equivalent.
11+
However, `||` is xref:appendix/gql-conformance/index.adoc[GQL conformant], while `+` is not.
1112

12-
For additional expressions evaluating `STRING` values, see xref:functions/string.adoc[String functions].
13+
For additional expressions that evaluate `STRING` values, see xref:functions/string.adoc[String functions].
1314

1415
[[examples]]
1516
== Examples
1617

1718
.`||` and `+`
1819
[source, cypher]
1920
----
20-
RETURN 'concatenatedWith' || '||' AS result1
21-
'concatenatedWith' + '+' AS result2
21+
RETURN 'Neo' || '4j' AS result1
22+
'Neo' + '4j' AS result2
2223
----
2324

2425
.Result
2526
[role="queryresult",options="header,footer",cols="2*<m"]
2627
|===
2728
| result1 | result2
2829

29-
| "concatenatedWith||" | "concatenatedWith+"
30+
| "Neo4j" | "Neo4j"
3031

3132
2+d|Rows: 1
3233
|===
3334

35+
The xref:functions/string.adoc#functions-tostring[`toString()`] function can be used to concatenate non-`STRING` values into a `STRING` value.
36+
37+
.Concatenation using `toString()` function
38+
[source, cypher]
39+
----
40+
RETURN 'The number is: ' || toString(42) AS result
41+
----
42+
43+
.Result
44+
[role="queryresult",options="header,footer",cols="1*<m"]
45+
|===
46+
| result
47+
48+
| "The number is: 42"
49+
50+
1+d| Rows: 1
51+
|===
52+
53+
Cypher does not insert spaces when concatenating `STRING` values.
54+
55+
.Adding whitespaces in `STRING` concatenation
56+
[source, cypher]
57+
----
58+
RETURN 'Alpha' || 'Beta' AS result1,
59+
'Alpha' || ' ' || 'Beta' AS result2
60+
----
61+
62+
.Result
63+
[role="queryresult",options="header,footer",cols="2*<m"]
64+
|===
65+
| result1 | result2
66+
67+
| "AlphaBeta" | "Alpha Beta"
68+
69+
1+d| Rows: 1
70+
|===
71+
72+
.Concatenating `STRING` properties
73+
[source, cypher]
74+
----
75+
CREATE (p:Person {firstName: 'Keanu', lastName: 'Reeves'})
76+
SET p.fullName = p.firstName || ' ' || p.lastName
77+
RETURN p.fullName AS fullName
78+
----
79+
80+
.Result
81+
[role="queryresult",options="header,footer",cols="1*<m"]
82+
|===
83+
| fullName
84+
85+
| "Keanu Reeves"
86+
87+
1+d| Rows: 1
88+
|===
89+
90+
`STRING` values in a `LIST` can be concatenated using the xref:functions/list.adoc#functions-reduce[`reduce()`] function.
91+
92+
.Concatenate `STRING` values in a `LIST`
93+
[source, cypher]
94+
----
95+
RETURN reduce(acc = '', item IN ['Neo', '4j'] | acc + item) AS result
96+
----
97+
98+
.Result
99+
[role="queryresult",options="header,footer",cols="1*<m"]
100+
|===
101+
| result
102+
103+
| "Neo4j"
104+
105+
1+d| Rows: 1
106+
|===
107+
108+
Concatenating a `STRING` value with `NULL` returns `NULL`.
109+
To skip the first `NULL` value in a list of expressions, use the xref:functions/scalar.adoc#functions-coalesce[`coalesce()`] function.
110+
111+
In the following example, `coalesce()` is used with `reduce()` to replace each `NULL` value in the `LIST` with an empty `STRING` (`''`).
112+
This ensures that all `NULL` values are effectively skipped, allowing the `reduce()` function to concatenate the remaining `STRING` values.
113+
114+
.Using `reduce()` and `coalesce()` to skip `NULL` values when concatenating a `LIST`
115+
[source, cypher]
116+
----
117+
WITH ['Neo', NULL, '4j', NULL, 'Hello', NULL] AS items
118+
RETURN reduce(acc = '', item IN items | acc + coalesce(item, '')) AS result
119+
----
120+
121+
.Result
122+
[role="queryresult",options="header,footer",cols="1*<m"]
123+
|===
124+
| result
34125

126+
| "Neo4jHello"
35127

128+
1+d| Rows: 1
129+
|===

0 commit comments

Comments
 (0)