|
1 | | -= String operators |
| 1 | += String concatenation operators |
2 | 2 | :description: Information about Cypher's string operators. |
3 | 3 | :table-caption!: |
4 | 4 |
|
5 | | -Cypher contains two functionally equivalent operators for the concatenation of `STRING` values: |
| 5 | +Cypher contains two operators for the concatenation of `STRING` values: |
6 | 6 |
|
7 | | -* `+` |
8 | 7 | * `||` |
| 8 | +* `+` |
9 | 9 |
|
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. |
11 | 12 |
|
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]. |
13 | 14 |
|
14 | 15 | [[examples]] |
15 | 16 | == Examples |
16 | 17 |
|
17 | 18 | .`||` and `+` |
18 | 19 | [source, cypher] |
19 | 20 | ---- |
20 | | -RETURN 'concatenatedWith' || '||' AS result1 |
21 | | - 'concatenatedWith' + '+' AS result2 |
| 21 | +RETURN 'Neo' || '4j' AS result1 |
| 22 | + 'Neo' + '4j' AS result2 |
22 | 23 | ---- |
23 | 24 |
|
24 | 25 | .Result |
25 | 26 | [role="queryresult",options="header,footer",cols="2*<m"] |
26 | 27 | |=== |
27 | 28 | | result1 | result2 |
28 | 29 |
|
29 | | -| "concatenatedWith||" | "concatenatedWith+" |
| 30 | +| "Neo4j" | "Neo4j" |
30 | 31 |
|
31 | 32 | 2+d|Rows: 1 |
32 | 33 | |=== |
33 | 34 |
|
| 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 |
34 | 125 |
|
| 126 | +| "Neo4jHello" |
35 | 127 |
|
| 128 | +1+d| Rows: 1 |
| 129 | +|=== |
0 commit comments