SQL Clone
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


select order and comma delimited list


select order and comma delimited list

Author
Message
kevin 20860
kevin 20860
Grasshopper
Grasshopper (10 reputation)Grasshopper (10 reputation)Grasshopper (10 reputation)Grasshopper (10 reputation)Grasshopper (10 reputation)Grasshopper (10 reputation)Grasshopper (10 reputation)Grasshopper (10 reputation)

Group: General Forum Members
Points: 10 Visits: 34
Hi all,
Have an issue with a comma delimited list which is giving me a headache. Any suggestions would be appreciated.

Delimited list 1,3,2

I need to take each item from the list and match it to another table of names which in turn will be comma delimited.

mytable
ID Name
1 Fred
2 Joe
3 Bob

The result I'm looking for is: Fred,Bob, Joe

What's I've done is the following:
declare @path varchar(100)
select @path = '1,3,2'
stuff((select name from mytable where id in (select id FROM dbo.ufn_split(path) for xml path('')),1,1,'')

This works except that the "select ..where in " changes the order. Ie the names that are pulled from mytable are not in the same order as the id's in @path.

For clarity if you break down my code to:

select name from mytable where id in (select id FROM dbo.ufn_split(path))

it will give you a different order to this:

select id FROM dbo.ufn_split(path)


Any ideas?
thanks
Abu Dina
Abu Dina
Ten Centuries
Ten Centuries (1.3K reputation)Ten Centuries (1.3K reputation)Ten Centuries (1.3K reputation)Ten Centuries (1.3K reputation)Ten Centuries (1.3K reputation)Ten Centuries (1.3K reputation)Ten Centuries (1.3K reputation)Ten Centuries (1.3K reputation)

Group: General Forum Members
Points: 1259 Visits: 3323
tut tut tut.... don't cross-post Kevin! ;-)

http://www.sqlservercentral.com/Forums/Topic1419967-8-1.aspx

---------------------------------------------------------


It takes a minimal capacity for rational thought to see that the corporate 'free press' is a structurally irrational and biased, and extremely violent, system of elite propaganda.
David Edwards - Media lens

Society has varying and conflicting interests; what is called objectivity is the disguise of one of these interests - that of neutrality. But neutrality is a fiction in an unneutral world. There are victims, there are executioners, and there are bystanders... and the 'objectivity' of the bystander calls for inaction while other heads fall.
Howard Zinn
kevin 20860
kevin 20860
Grasshopper
Grasshopper (10 reputation)Grasshopper (10 reputation)Grasshopper (10 reputation)Grasshopper (10 reputation)Grasshopper (10 reputation)Grasshopper (10 reputation)Grasshopper (10 reputation)Grasshopper (10 reputation)

Group: General Forum Members
Points: 10 Visits: 34
Sorry. The other one is in the wrong forum. I went back to delete it but there is no "delete" option. Should be in this forum only.

thanks,
Cadavre
Cadavre
Hall of Fame
Hall of Fame (3.9K reputation)Hall of Fame (3.9K reputation)Hall of Fame (3.9K reputation)Hall of Fame (3.9K reputation)Hall of Fame (3.9K reputation)Hall of Fame (3.9K reputation)Hall of Fame (3.9K reputation)Hall of Fame (3.9K reputation)

Group: General Forum Members
Points: 3936 Visits: 8472
Sample data: -
IF object_id('tempdb..#testEnvironment') IS NOT NULL
BEGIN
DROP TABLE #testEnvironment;
END;

SELECT ID, Name
INTO #testEnvironment
FROM (VALUES(1, 'Fred'),(2, 'Joe'),(3, 'Bob'))a(ID, Name);



Query: -
DECLARE @Delimitedlist VARCHAR(8000) = '1,3,2';

SELECT STUFF((SELECT ','+a.Name
FROM #testEnvironment a
INNER JOIN (SELECT ItemNumber AS Ordering, Item
FROM dbo.DelimitedSplit8K(@Delimitedlist,',')
) b ON a.ID = b.Item
ORDER BY b.Ordering
FOR XML PATH(''), TYPE).value('.','NVARCHAR(MAX)'),1,1,'');



Result: -
Fred,Bob,Joe


dbo.DelimitedSplit8K function can be found here --> http://www.sqlservercentral.com/articles/Tally+Table/72993/.

Solution will only work if your original deliminated string is never more than 8000 characters.


Forever trying to learn

For better, quicker answers on T-SQL questions, click on the following...
http://www.sqlservercentral.com/articles/Best+Practices/61537/

For better, quicker answers on SQL Server performance related questions, click on the following...
http://www.sqlservercentral.com/articles/SQLServerCentral/66909/



If you litter your database queries with nolock query hints, are you aware of the side effects?
Try reading a few of these links...

(*) Missing rows with nolock
(*) Allocation order scans with nolock
(*) Consistency issues with nolock
(*) Transient Corruption Errors in SQL Server error log caused by nolock
(*) Dirty reads, read errors, reading rows twice and missing rows with nolock


Craig Wilkinson - Software Engineer
LinkedIn
kevin 20860
kevin 20860
Grasshopper
Grasshopper (10 reputation)Grasshopper (10 reputation)Grasshopper (10 reputation)Grasshopper (10 reputation)Grasshopper (10 reputation)Grasshopper (10 reputation)Grasshopper (10 reputation)Grasshopper (10 reputation)

Group: General Forum Members
Points: 10 Visits: 34
Thanks for that. Worked perfectly.
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search