1

How to transpose CSV string in rows? In the example below, rows are transposed into CSV string but I would like to do the inverse operation (split this string "Day,Evening,Night" into rows)?. How to do that?

USE AdventureWorks
GO
-- Check Table Column
SELECT [Name]
FROM HumanResources.Shift
GO
-- Get CSV values
SELECT STUFF(
(SELECT ',' + s.Name
FROM HumanResources.Shift s
ORDER BY s.Name
FOR XML PATH('')),1,1,'') AS CSV
GO

The example is taken from here.

1
  • I edited my question to make it more clear Commented Apr 17, 2011 at 6:44

2 Answers 2

1

There are several techniques in the canonical reference here: Arrays and Lists in SQL Server 2005 and Beyond by Erland Sommarskog

We use the numbers table technique

Sign up to request clarification or add additional context in comments.

Comments

1

I would recommend using UDF written in C# - relatively easy to develop (if you're familiar with C#) and the fastest choice when it comes to execution.

Reading a reference provided by gbn will give much deeper understanding of the subject, though will certainly take longer.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.