cancel
Showing results for 
Search instead for 
Did you mean: 

Join the community at Nodes 2022, our free virtual event on November 16 - 17.

Concatenate list in a string

dhanashree_murg
Node Link

Hi All,

I want to do a simple concatenation of list to get string value, However I am struggling to do the same. For functionality I want to implement I have requirement of concatenating multiple columns together, but one of the column is coming from aggregate function collect, which is preventing me from concatenating values of a row. I am trying to do something like this:

with [2,4,5,7] as a, "test" as name
unwind a as x
return name + " | " +  toString(collect(toString(x)))

This is syntactically wrong but expected output is like

"test | 2,4,5,7"

Can someone please help me? Thank you in advance
P.S this is just one row example, there are going to be multiple rows with different a's and different name

1 ACCEPTED SOLUTION

Cobra
Ninja
Ninja

Hello @dhanashree.murge

You can use apoc.text.join() function from APOC plugin:

WITH [2, 4, 5, 7] AS list, "test" AS name
RETURN name + " | " + apoc.text.join([i IN list | toString(i)], ",")

Regards,
Cobra

View solution in original post

4 REPLIES 4

Cobra
Ninja
Ninja

Hello @dhanashree.murge

You can use apoc.text.join() function from APOC plugin:

WITH [2, 4, 5, 7] AS list, "test" AS name
RETURN name + " | " + apoc.text.join([i IN list | toString(i)], ",")

Regards,
Cobra

dhanashree_murg
Node Link

Thank you @Cobra , it worked!
also just curious, is there a way to do it without APOC? does cypher support for loops? Foreach is specific for cypher commands like SET, CREATE etc

Cobra
Ninja
Ninja

Yeah it's possible but less easy:

WITH [2, 4, 5, 7] AS list, "test" AS name
WITH name + " | " +  reduce(tmp = "", i IN list | tmp + toString(i) + ",") AS value
RETURN substring(value, 0, size(value)-1)

dhanashree_murg
Node Link

That's Awesome @Cobra , Lot of thanks!! 🙂