Login or Sign Up to become a member!
LessThanDot Sit Logo

LessThanDot

Community Wiki

Less Than Dot is a community of passionate IT professionals and enthusiasts dedicated to sharing technical knowledge, experience, and assistance. Inside you will find reference materials, interesting technical discussions, and expert tips and commentary. Once you register for an account you will have immediate access to the forums and all past articles and commentaries.

LTD Social Sitings

Lessthandot twitter Lessthandot Linkedin Lessthandot friendfeed Lessthandot facebook Lessthandot rss

Note: Watch for social icons on posts by your favorite authors to follow their postings on these and other social sites.

Navigation

Google Ads

Concatenate Values From Multiple Rows Into One Column Ordered

From Wiki

Jump to: navigation, search

If you want to concatenate values from multiple rows into one and you want to order it then you have to use FOR XML PATH. The ORDER BY is -not- guaranteed to be processed before the concatenation occurs, if you use that technique. However, it -is- guaranteed if you use FOR XML PATH('').

Let's take a look. First create these tables:

  1. USE TEMPDB
  2. GO
  3. CREATE TABLE Authors (Id INT, LastName VARCHAR(100), FirstName VARCHAR(100))
  4. GO
  5. INSERT Authors VALUES(1,'Nielsen','Paul')
  6. INSERT Authors VALUES(2,'King','Stephen')
  7. INSERT Authors VALUES(3,'Stephenson','Neal')
  8. GO
  9.  
  10. CREATE TABLE Books (ID INT,AuthorID INT, BookName VARCHAR(200))
  11. GO
  12. INSERT Books VALUES(1,1,'SQL Server 2005 Bible')
  13. INSERT Books VALUES(2,1,'SQL Server 2008 Bible')
  14.  
  15. INSERT Books VALUES(3,2,'It')
  16. INSERT Books VALUES(4,2,'The Stand')
  17. INSERT Books VALUES(5,2,'Thinner')
  18. INSERT Books VALUES(6,2,'Salems Lot')
  19.  
  20.  
  21. INSERT Books VALUES(7,3,'Snow Crash')
  22. INSERT Books VALUES(8,3,'The Diamond Age')
  23. INSERT Books VALUES(9,3,'Cryptonomicon')
  24. GO

This is the old style function

  1. CREATE FUNCTION fnGetBooks2 (@AuthorID INT)
  2. RETURNS VARCHAR(8000)
  3. AS
  4. BEGIN
  5.         DECLARE @BookList VARCHAR(8000)
  6.         SELECT @BookList = ''
  7.         SELECT @BookList = @BookList + BookName +','
  8.         FROM Books
  9.         WHERE AuthorID = @AuthorID
  10.         AND BookName IS NOT NULL
  11.         ORDER BY BookName --yes we can sort
  12.  
  13. RETURN LEFT(@BookList,(LEN(@BookList) -1))
  14. END
  15. GO


This is the same function using XML Path

  1. CREATE FUNCTION fnGetBooks (@AuthorID INT)
  2.  
  3. RETURNS VARCHAR(8000)
  4. AS
  5. BEGIN
  6.        
  7.  DECLARE @BookList VARCHAR(8000)
  8.  SELECT @BookList =(
  9.  
  10.         SELECT  BookName + ', ' AS [TEXT()]
  11.  
  12.      FROM    Books
  13.  WHERE AuthorID = @AuthorID
  14.         AND BookName IS NOT NULL
  15.      ORDER BY BookName
  16.  
  17.      FOR XML PATH('') )
  18.        
  19.  
  20. RETURN LEFT(@BookList,(LEN(@BookList) -1))
  21. END
  22. GO


  1. SELECT *,dbo.fnGetBooks(id) AS Books
  2. FROM Authors
  3. Results


  1. SELECT *,dbo.fnGetBooks2(id) AS Books
  2. FROM Authors
  3. Results


Id LastName FirstName Books
1 Nielsen Paul SQL Server 2005 Bible,SQL Server 2008 Bible
2 King Stephen It,Salems Lot,The Stand,Thinner
3 Stephenson Neal Cryptonomicon,Snow Crash,The Diamond Age

The results are the same but I have been assured by SQL Server MVPs that FOR XML PATH will preserve the order while the other function might not.

For detailed explanation of this technique take a look at Brad Schulz blog

Making a list and checking it twice

Contributed by: --SQLDenis 02:32, 31 May 2008 (GMT)

Part of SQL Server Programming Hacks

Section Sorting, Limiting

285 Rating: 3.1/5 (16 votes cast)