Avatar of maqskywalker
maqskywalker
 asked on

Working with comma separated strings in a sql server table

Hi Experts,

I'm using the Employees table from the Northwind Traders sample database.

I'm only using these columns.

Employees.PNG


If I use this query

SELECT [name] AS ColNames 
FROM syscolumns 
WHERE id=OBJECT_ID('Employees')
AND [colid] < 4
ORDER BY [colid]

Open in new window


I get the column names

ColumnNames.PNG


If I use the STRING_AGG function query, i get this string:

SELECT '(' + STRING_AGG([EmployeeID], ',') + '),' 
     + '(' + STRING_AGG([LastName], ',') + '),'
     + '(' + STRING_AGG([FirstName], ',') + ')' AS [StringCol]
FROM [Northwind].[dbo].[Employees]

Open in new window


CurrentResult.PNG
 


What's an elegant way to get this result ?

I want to add the column name to the beginning of each part of the string like on this mockup image.

One way i was thinking, is before i even use the the STRING_AGG, in a select statement select all columns and add the column name to row 1 of each column,  
Then once I added the column name to row 1 of every column than i could use STRING_AGG. But not sure if that is a good approach.



 StringListWithColumName.PNGstringcolumns.PNG
Microsoft SQL ServerSQL

Avatar of undefined
Last Comment
Mark Wills

8/22/2022 - Mon
ASKER CERTIFIED SOLUTION
Ryan Chong

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.
maqskywalker

ASKER
Thanks for the info. It's very helpful to learn this syntax.
Mark Wills

Could also use :

SELECT '(Employee_ID,' + STRING_AGG([EmployeeID], ',') + '),' 
     + '(LastName,' + STRING_AGG([LastName], ',') + '),'
     + '(FirstName,' + STRING_AGG([FirstName], ',') + ')' AS [StringCol]
FROM [Northwind].[dbo].[Employees]

Open in new window

And should probably check for NULL so that they always align....
slightwv (䄆 Netminder)

You have asked several related questions.  What is your end goal for this data?  You might look at a more commonly accepted data interchange method like JSON or XML instead of some hybrid CSV with parans, etc...
I started with Experts Exchange in 2004 and it's been a mainstay of my professional computing life since. It helped me launch a career as a programmer / Oracle data analyst
William Peck
maqskywalker

ASKER
slightwv,

I have a csv file that was exported from a process.
But the csv files contains only pieces of data i need. It's not a straight data dump. In the csv it only has parts of the data.
For example the csv file might contain only the vendor last name and purchase date for an order.
Then I would use that vendor last name and purchase data info from the csv file and look up the order details from an existing sql server table and from the query result set,  transfer those order details data into a different existing table.

For some thing like that would you recommend using JSON or XML?
slightwv (䄆 Netminder)

So how is writing SQL that generates a CSV string like "(a,b,c),(d,e,f)" going to help get data into some destination table?

Seems like pretty straight forward ETL.  Load the columns you want from the original CSV into a staging table.

Then it might be as simple:  Insert into destination table ( select ...);
Mark Wills

Sounds like there could be another new question happening pretty soon :)

Maybe with some sample data ? Edited of course so we can't see any data resembling "real life", more a symbolic representation of data..
⚡ FREE TRIAL OFFER
Try out a week of full access for free.
Find out why thousands trust the EE community with their toughest problems.