Solved

Convert Comma Seporated Column Into Rows in SQL

Posted on 2014-04-17
2
507 Views
Last Modified: 2014-04-17
I have a code that will do what will do this the problem is it has a limit of 100 comma separate field that it can do.  I need on that handle anywhere from 1 to 500 or more.

Here is the code that i am currently useing.
;with tmp(VEND_ITEM_NO, MFG, MODEL, Data) as (
select VEND_ITEM_NO, MFG, LEFT(MODEL, CHARINDEX(',',MODEL+',')-1),
    STUFF(MODEL, 1, CHARINDEX(',',MODEL+','), '')
from USER_TEMP_TABLE1 where MFG in ('SONY')
union all
select VEND_ITEM_NO, MFG, LEFT(Data, CHARINDEX(',',Data+',')-1),
    STUFF(Data, 1, CHARINDEX(',',Data+','), '')
from tmp
where Data > ''
)
select VEND_ITEM_NO, MFG, MODEL
from tmp
order by VEND_ITEM_NO

Open in new window


Attached is a example of how the data looksexample.xlsx
0
Comment
Question by:Littlet5621
2 Comments
 
LVL 33

Expert Comment

by:Big Monty
ID: 40007040
i use the following function to split a comma delimited string into a temp table and have never run into any column constraints:

CREATE FUNCTION dbo.SplitStrings
(
   @List NVARCHAR(MAX),
   @Delimiter NVARCHAR(255)
)
RETURNS TABLE
WITH SCHEMABINDING AS
RETURN
  WITH E1(N)        AS ( SELECT 1 UNION ALL SELECT 1 UNION ALL SELECT 1 UNION ALL SELECT 1 
                         UNION ALL SELECT 1 UNION ALL SELECT 1 UNION ALL SELECT 1 
                         UNION ALL SELECT 1 UNION ALL SELECT 1 UNION ALL SELECT 1),
       E2(N)        AS (SELECT 1 FROM E1 a, E1 b),
       E4(N)        AS (SELECT 1 FROM E2 a, E2 b),
       E42(N)       AS (SELECT 1 FROM E4 a, E2 b),
       cteTally(N)  AS (SELECT 0 UNION ALL SELECT TOP (DATALENGTH(ISNULL(@List,1))) 
                         ROW_NUMBER() OVER (ORDER BY (SELECT NULL)) FROM E42),
       cteStart(N1) AS (SELECT t.N+1 FROM cteTally t
                         WHERE (SUBSTRING(@List,t.N,1) = @Delimiter OR t.N = 0))
  SELECT Item = SUBSTRING(@List, s.N1, ISNULL(NULLIF(CHARINDEX(@Delimiter,@List,s.N1),0)-s.N1,8000))
    FROM cteStart s;

Open in new window

0
 
LVL 8

Accepted Solution

by:
ProjectChampion earned 500 total points
ID: 40007107
That's because your script is recursive and by default the max depth of recursion is 100; but you can override it using MAXRECURSION hint, for instance:

SELECT ....
....
WHERE ...
OPTION  ( MAXRECURSION 1000 )
0

Featured Post

Ransomware: The New Cyber Threat & How to Stop It

This infographic explains ransomware, type of malware that blocks access to your files or your systems and holds them hostage until a ransom is paid. It also examines the different types of ransomware and explains what you can do to thwart this sinister online threat.  

Question has a verified solution.

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

Suggested Solutions

Occasionally there is a need to clean table columns, especially if you have inherited legacy data. There are obviously many ways to accomplish that, including elaborate UPDATE queries with anywhere from one to numerous REPLACE functions (even within…
In this article I will describe the Detach & Attach method as one possible migration process and I will add the extra tasks needed for an upgrade when and where is applied so it will cover all.
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.
Using examples as well as descriptions, and references to Books Online, show the documentation available for datatypes, explain the available data types and show how data can be passed into and out of variables.

766 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