Solved

Convert Comma Seporated Column Into Rows in SQL

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

Migrating Your Company's PCs

To keep pace with competitors, businesses must keep employees productive, and that means providing them with the latest technology. This document provides the tips and tricks you need to help you migrate an outdated PC fleet to new desktops, laptops, and tablets.

Question has a verified solution.

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

In this article I will describe the Copy Database Wizard 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.
For both online and offline retail, the cross-channel business is the most recent pattern in the B2C trade space.
Using examples as well as descriptions, and references to Books Online, show the different Recovery Models available in SQL Server and explain, as well as show how full, differential and transaction log backups are performed
Via a live example, show how to set up a backup for SQL Server using a Maintenance Plan and how to schedule the job into SQL Server Agent.

726 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