?
Solved

Convert Comma Seporated Column Into Rows in SQL

Posted on 2014-04-17
2
Medium Priority
?
514 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
[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
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 1500 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

Get 15 Days FREE Full-Featured Trial

Benefit from a mission critical IT monitoring with Monitis Premium or get it FREE for your entry level monitoring needs.
-Over 200,000 users
-More than 300,000 websites monitored
-Used in 197 countries
-Recommended by 98% of users

Question has a verified solution.

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

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…
In the first part of this tutorial we will cover the prerequisites for installing SQL Server vNext on Linux.
Familiarize people with the process of utilizing SQL Server functions from within Microsoft Access. Microsoft Access is a very powerful client/server development tool. One of the SQL Server objects that you can interact with from within Microsoft Ac…
Via a live example combined with referencing Books Online, show some of the information that can be extracted from the Catalog Views in SQL Server.
Suggested Courses

752 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