Solved

Convert Comma Seporated Column Into Rows in SQL

Posted on 2014-04-17
2
504 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 32

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

Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

Join & Write a Comment

Suggested Solutions

Introduction SQL Server Integration Services can read XML files, that’s known by every BI developer.  (If you didn’t, don’t worry, I’m aiming this article at newcomers as well.) But how far can you go?  When does the XML Source component become …
Ever needed a SQL 2008 Database replicated/mirrored/log shipped on another server but you can't take the downtime inflicted by initial snapshot or disconnect while T-logs are restored or mirror applied? You can use SQL Server Initialize from Backup…
Via a live example, show how to extract information from SQL Server on Database, Connection and Server properties
Via a live example, show how to setup several different housekeeping processes for a SQL Server.

757 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

Need Help in Real-Time?

Connect with top rated Experts

22 Experts available now in Live!

Get 1:1 Help Now