?
Solved

SQL server splitting a comma separated value

Posted on 2016-09-03
3
Medium Priority
?
40 Views
Last Modified: 2016-10-22
i want code for splitting a commaseparated list int a temporary table in sql server. can you help me
0
Comment
Question by:Gopakumar Pandarikkal
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
3 Comments
 
LVL 35

Accepted Solution

by:
ste5an earned 1000 total points
ID: 41782808
There are two built-in ways:

DECLARE @Text NVARCHAR(255) = N'1,2,<td>test</td>,4';

-- SQL Server 2016
SELECT	SS.*
FROM	STRING_SPLIT(@Text, ',') SS;

-- Before
WITH ToXml AS
	(
		SELECT	CAST(('<td><![CDATA[' + REPLACE(@Text, ',', ']]></td><td><![CDATA[') + ']]></td>') AS XML) AS XmlData
	)
	SELECT	td.value('.', 'NVARCHAR(255)') AS [value]
	FROM	ToXml
		CROSS APPLY	XmlData.nodes('td') A ( td );

Open in new window


The XML method is not optimal, but for normal purposes fast enough.
2
 
LVL 41

Assisted Solution

by:Sharath
Sharath earned 1000 total points
ID: 41787495
try this.
DECLARE  @s VARCHAR(100)

SELECT @s = 'Apples,Oranges,Mangoes,Grapes';

WITH cte
     AS (SELECT ROW_NUMBER()
                  OVER(ORDER BY p1.NUMBER) AS sr,
                NUMBER
         FROM   MASTER..spt_values p1
         WHERE  p1.TYPE = 'p'
                AND p1.NUMBER BETWEEN 1 AND LEN(@s) + 2
                AND SUBSTRING(',' + @s + ',',p1.NUMBER,1) = ',')
SELECT SUBSTRING(' ' + @s,a.NUMBER + 1,b.NUMBER - a.NUMBER - 1) AS Fruit
FROM   cte a
       INNER JOIN cte b
         ON b.sr = a.sr + 1

/*
Fruit
Apples
Oranges
Mangoes
Grapes
*/

Open in new window

0

Featured Post

Simplifying Server Workload Migrations

This use case outlines the migration challenges that organizations face and how the Acronis AnyData Engine supports physical-to-physical (P2P), physical-to-virtual (P2V), virtual to physical (V2P), and cross-virtual (V2V) migration scenarios to address these challenges.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Slowly Changing Dimension Transformation component in data task flow is very useful for us to manage and control how data changes in SSIS.
Load balancing is the method of dividing the total amount of work performed by one computer between two or more computers. Its aim is to get more work done in the same amount of time, ensuring that all the users get served faster.
Using examples as well as descriptions, and references to Books Online, show the documentation available for date manipulation functions and by using a select few of these functions, show how date based data can be manipulated with these functions.
Via a live example, show how to extract information from SQL Server on Database, Connection and Server properties
Suggested Courses

777 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question