-2

I have a table in SQLite which contains some data.
Like this

Table id
       1
       2  
       3
       4
       5
       6
       7

what i want is a String which contains all the ids separated by a semicolon (;) character, like

 1;2;3;4;5;6;7

I know there is group concat.
but I cannot figure out how to do that with this sample.

And there is another method called COALESCE I found here, but in SQLite we cannot declare variables.

Is there any way to do this in SQLite?

Community
  • 1
  • 1
max
  • 5,963
  • 12
  • 49
  • 80

1 Answers1

0

You can use aggregation function group_concat(X,Y) to achieve it:

The group_concat() function returns a string which is the concatenation of all non-NULL values of X. If parameter Y is present then it is used as the separator between instances of X. A comma (",") is used as the separator if Y is omitted. The order of the concatenated elements is arbitrary.

SELECT GROUP_CONCAT(id, ';') AS result 
FROM tab_name
-- WHERE id < 10; -- if needed

If you don't specify GROUP BY ... clause all records will be treated as one group.

Lukasz Szozda
  • 162,964
  • 23
  • 234
  • 275