Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2008 Forums
 Transact-SQL (2008)
 Possible to Sort this?

Author  Topic 

gwjones12
Starting Member

9 Posts

Posted - 2011-04-25 : 14:49:12
This function works great, it returns a list of all Organizations.OrgName rows related to the passed ID in a single line. What I need is for the OrgName list to be sorted. How?

Thanks


ALTER FUNCTION [dbo].[GetOrgNamesByListID] (@IDs varchar(500))
RETURNS varchar(max) AS

BEGIN

declare @output varchar(max)
select @output = COALESCE(@output + ', ', '') + OrgName
from dbo.Organizations INNER JOIN
(select ltrim(rtrim(mynode.value('.[1]', 'nvarchar(12)'))) as ID
from (select cast('<v>'+ replace(@IDs,',','</v><v>') + '</v>' as xml) doc) xx
cross apply doc.nodes('/v') (mynode)
) as tbl ON
OrganizationID = tbl.ID

return @output

END

tkizer
Almighty SQL Goddess

38200 Posts

Posted - 2011-04-25 : 14:51:28
Did you try adding an ORDER BY for OrgName?

Tara Kizer
Microsoft MVP for Windows Server System - SQL Server
http://weblogs.sqlteam.com/tarad/

Subscribe to my blog
Go to Top of Page

gwjones12
Starting Member

9 Posts

Posted - 2011-04-26 : 11:12:01
quote:
Originally posted by tkizer

Did you try adding an ORDER BY for OrgName?

Tara Kizer



Humm, I was sure that it didn't work when I tried it before, but it does now!

declare @output varchar(max)
select @output = COALESCE(@output + ', ', '') + OrgName
from dbo.Organizations INNER JOIN
(select ltrim(rtrim(mynode.value('.[1]', 'nvarchar(12)'))) as ID
from (select cast('<v>'+ replace(@IDs,',','</v><v>') + '</v>' as xml) doc) xx
cross apply doc.nodes('/v') (mynode)
) as tbl ON
OrganizationID = tbl.ID
order by orgname


Go to Top of Page

tkizer
Almighty SQL Goddess

38200 Posts

Posted - 2011-04-26 : 12:39:58


Tara Kizer
Microsoft MVP for Windows Server System - SQL Server
http://weblogs.sqlteam.com/tarad/

Subscribe to my blog
Go to Top of Page
   

- Advertisement -