Solved

Split comma sperated values to multiple records

Posted on 2012-03-29
4
470 Views
Last Modified: 2012-03-29
I have a table which has columns like: (actual no of columns in table are 15)

P_ID,      C_ID,          PR_ID,      data
-------------------------------------------------
1               2                3           a,b,c,d,f,
2              3                 4            NULL
5              6                 3            L,M,

what I want is create a view which gives me record for each comma separated value, something like this:

1  2    3   a
1  2    3   b
1  2    3   c
1  2    3   d
1  2    3   f
2  3    4  NULL
5  6    3  L
5  6    3  M

I am using SQL 2008. And also have a UDF split function which is
dbo.Split('x,y,z,w',',') and returns a table like

Id      Value
1      x
2      y
3      z
4      w


I would like to make use of this function if possible.

Thanks for your help
0
Comment
Question by:immtrac
[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
4 Comments
 
LVL 69

Accepted Solution

by:
Scott Pletcher earned 500 total points
ID: 37783519
SELECT
    tn._ID, tn.C_ID, tn.PR_ID,  --...
    d.value
FROM dbo.table_name tn
CROSS APPLY dbo.Split(data) AS d
0
 
LVL 16

Expert Comment

by:vdr1620
ID: 37783542
Try this

CREATE TABLE #A
(
Col1 INT,
Col2 Varchar(10)
)

INSERT INTO #A VALUES (1,'a,b,c')
INSERT INTO #A VALUES (2, 'd,e')


 Select T.col1, Z.Position, Z.Value
        , Row_Number() Over ( Partition By T.Col1 Order By Z.Position ) As Num
    From #A As T
        Cross Apply dbo.ufnSplit( T.Col2, ',' ) As Z

Open in new window

0
 

Author Closing Comment

by:immtrac
ID: 37783556
Simple and it worked for me
0
 
LVL 69

Expert Comment

by:Scott Pletcher
ID: 37783601
To insure that the split values list in order, add an ORDER BY:


SELECT
    tn._ID, tn.C_ID, tn.PR_ID,  --...
    d.value
FROM dbo.table_name tn
CROSS APPLY dbo.Split(data, ',') AS d
ORDER BY
    tn._ID,
    d.Id
0

Featured Post

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

Suggested Solutions

The Delta outage: 650 cancelled flights, more than 1200 delayed flights, thousands of frustrated customers, tens of millions of dollars in damages – plus untold reputational damage to one of the world’s most trusted airlines. All due to a catastroph…
This article discusses how to create an extensible mechanism for linked drop downs.
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
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)

733 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