Question

I have a table with rows of data and need to create a comma delimited string for all the rows for a column. Can this be achieved just using the the SELECT statement in SQLite or I have to get the data into Cursor and build the string by iterating through it?

For Example:

UserId
1df4181d-6c52-4aa3-926f-2dacb0a68c70
1df4181d-6c52-4aa3-926f-2dacb0a68c71
1df4181d-6c52-4aa3-926f-2dacb0a68c72
1df4181d-6c52-4aa3-926f-2dacb0a68c73
1df4181d-6c52-4aa3-926f-2dacb0a68c74
1df4181d-6c52-4aa3-926f-2dacb0a68c75

into

1df4181d-6c52-4aa3-926f-2dacb0a68c70,1df4181d-6c52-4aa3-926f-2dacb0a68c71,1df4181d-6c52-4aa3-926f-2dacb0a68c72,1df4181d-6c52-4aa3-926f-2dacb0a68c73,1df4181d-6c52-4aa3-926f-2dacb0a68c74,1df4181d-6c52-4aa3-926f-2dacb0a68c75

Any suggestions would be greatly appreciated.

Was it helpful?

Solution

You can use GROUP_CONCAT():

select group_concat(yourColumn, ',')
from yourtable

See SQL Fiddle with Demo

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top