0

I have data which is coming from a sql query which has lots of joins and conditions attached to it,

now there are few columns where i want to bring the values of those columns as comma separated so it should not duplicate the row

i do not get if i had to write all joins in it and use stuff to get that values. or how can i use it, do i need to create a temporary table to it,

need some advice and if possible a practical example will help

Asum
  • 21
  • 5
  • 1
    You are describing `string_agg()` (available since SQL Server 2017). To get a more specific answer ,you would need to provide sample data and desired results as tabuler text, as well as your current query. – GMB Aug 31 '20 at 13:53
  • string_agg is only appropriate for numbers. otherwise, the strings need to be escaped if there are extra commas – SteveC Aug 31 '20 at 13:54
  • Json is a very good way to serialize from SQL because it takes care of delimiter issues – SteveC Aug 31 '20 at 13:56
  • i am on 2016, not 2017 and @steveC you talked about JSOn, but i still had to use stuff to have all the joins to 3 or 5 columns i have – Asum Aug 31 '20 at 14:00
  • Could we see the code? :) – SteveC Aug 31 '20 at 14:02
  • I marked this as a duplicate, not because your *question* is an exact duplicate, but because the answers are totally relevant to you, and I've used them successfully. Gnarly they are, but functional. Advice: upgrade to 2017 or better and use string_agg(). Advice, choose a delimiter that does not appear in any of your strings so you don't have to do any character-escaping mischigoss. – O. Jones Aug 31 '20 at 14:05
  • not my decision to update to 2017. – Asum Aug 31 '20 at 14:27
  • Using a delimiter that does not also naturally appear in your data is key. Asking just about any software to understand which columns are important in `a,"b,c","de,f","g",h` is unreliable and unpredictable, but especially SQL Server. This is Excel's job and even it can't get it right 100% of the time. Set SQL Server up for success, not for failure. – Aaron Bertrand Aug 31 '20 at 14:39
  • Hi, I added an answer to the question referenced in the closing. It contains different way to serialize, deserialize and store nested JSON arrays. JSON features were added in 2016 which is the OP's version – SteveC Aug 31 '20 at 16:26

0 Answers0