Link to home
Start Free TrialLog in
Avatar of maqskywalker
maqskywalker

asked on

t-sql display query result as list of comma separated values.

I'm using the Employees table from the Northwind Sql Server database.

It looks like this:

User generated image
If my input is this

DECLARE @LastName varchar(50)
SET @LastName = 'Davolio,Fuller,Leverling,Peacock,Buchanan,Suyama,King,Callahan,Dodsworth'

what is the syntax to get this result. Which is a list of the EmployeeIDs of the last names I input.

User generated image
Avatar of Scott Pletcher
Scott Pletcher
Flag of United States of America image

I don't think that name list will actually fit into 50 bytes :-).

You can use "standard" XML concat method of the lookup results, but it's high overhead, and a bit obscure.

What version of SQL? 2008/2012/2016/etc.
What's the most names you'd ever put in the list?
Avatar of maqskywalker
maqskywalker

ASKER

Using sql server 2008.  the datatype for the string list is varchar(2500)  that's the limit we are setting.
slightwv, no it's slightly different.
ASKER CERTIFIED SOLUTION
Avatar of Sharath S
Sharath S
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Thanks. Nice solution.