Solved

Split field into multiple rows

Posted on 2010-11-10
8
636 Views
Last Modified: 2012-05-10
Hello All,

I have the following data:

create table #products
(resort varchar(20)
, resv_name_id int
, allotment_header_id int
, grpvtrnt varchar(1)
, products varchar(200)
)
insert into #products (resort, resv_name_id , allotment_header_id , grpvtrnt , products) VALUES ('MTWASH', 34202, 16711, 'G', 'A, A, B, C, D, E')
insert into #products (resort, resv_name_id , allotment_header_id , grpvtrnt , products) VALUES ('MTWASH', 34203, 16711, 'G', 'A, B, C')
insert into #products (resort, resv_name_id , allotment_header_id , grpvtrnt , products) VALUES ('MTWASH', 34204, 16711, 'G', 'A, B, C, D')
insert into #products (resort, resv_name_id , allotment_header_id , grpvtrnt , products) VALUES ('MTWASH', 34205, 16711, 'G', 'A')

select * from #products

What I need at the end is one row per distinct product by resv_name_id
resort      resv_name_id      allotment_header_id      grpvtrnt      products
MTWASH      34202      16711      G      A
MTWASH      34202      16711      G      B
MTWASH      34202      16711      G      C
MTWASH      34202      16711      G      D
MTWASH      34202      16711      G      E
MTWASH      34203      16711      G      A
MTWASH      34203      16711      G      B
MTWASH      34203      16711      G      C
MTWASH      34204      16711      G      A
MTWASH      34204      16711      G      B
MTWASH      34204      16711      G      C
MTWASH      34204      16711      G      D
MTWASH      34205      16711      G      A
0
Comment
Question by:angelnjj
  • 4
  • 3
8 Comments
 
LVL 35

Expert Comment

by:YZlat
ID: 34105147
use Split function
0
 
LVL 35

Expert Comment

by:YZlat
ID: 34105158
0
 
LVL 1

Author Comment

by:angelnjj
ID: 34105294
I found that, but can't figure out how to incorporate into my select * from #products since it needs the split to be in the from clause.

Original:
select top 10 * from dbo.split('Chennai,Bangalore,Mumbai',',')

In select resort, resv_name_id, allotment_header_id, grpvtrnt, products from #products, where would I put the dbo.split?
0
 
LVL 1

Author Comment

by:angelnjj
ID: 34105299
select resort, resv_name_id, allotment_header_id, grpvtrnt, dbo.split(products,',') as products from #products isn't working.
0
DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

 
LVL 35

Expert Comment

by:YZlat
ID: 34105332
0
 
LVL 35

Expert Comment

by:YZlat
ID: 34105360
0
 
LVL 41

Accepted Solution

by:
ralmada earned 500 total points
ID: 34105426
First create the split function below, then use it in your query like this:
select a.*, b.Value from #products a
cross apply dbo.parmstolist(a.products, ',') b

create FUNCTION [dbo].[ParmsToList] (@Parameters varchar(8000), @delimiter varchar(10) ) 

returns @result TABLE (Value varchar(500), rn int identity) 

AS 

begin 

declare @dx varchar(9) 



DECLARE @TempList table 

( 

Value varchar(500) 

) 



if @delimiter is null set @delimiter = ' ' 

if len(@delimiter) < 1 set @delimiter = ' ' 

set @dx = left(@delimiter, case when @delimiter = ' ' then 1 else len(@delimiter) end -1) 



DECLARE @Value varchar(8000), @Pos int 



SET @Parameters = LTRIM(RTRIM(@Parameters))+ @delimiter 

SET @Pos = CHARINDEX(@delimiter, @Parameters, 1) 



IF REPLACE(@Parameters, @delimiter, @dx) <> '' 

BEGIN 

WHILE @Pos > 0 -- AND @Loops < 100 

BEGIN 

--set @loops = @loops + 1 

SET @Value = LTRIM(RTRIM(LEFT(@Parameters, @Pos - 1))) 

IF @Value <> '' 

BEGIN 

INSERT INTO @TempList (Value) VALUES (@Value) --Use Appropriate conversion 

END 

SET @Parameters = SUBSTRING(@Parameters, @Pos+ case when @delimiter = ' ' then 1 else len(@delimiter) end, 8000) 

SET @Pos = CHARINDEX(@delimiter, @Parameters, 1) 



END 

END 

INSERT @result 

SELECT value 

FROM @TempList 

RETURN 

END

Open in new window

0
 
LVL 1

Author Closing Comment

by:angelnjj
ID: 34106330
Thank you...how to use it was what I was missing - didn't understand need for cross apply, thanks for the example (used a different function).
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Introduction: When running hybrid database environments, you often need to query some data from a remote db of any type, while being connected to your MS SQL Server database. Problems start when you try to combine that with some "user input" pass…
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…
Here's a very brief overview of the methods PRTG Network Monitor (https://www.paessler.com/prtg) offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…
This tutorial demonstrates a quick way of adding group price to multiple Magento products.

919 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

16 Experts available now in Live!

Get 1:1 Help Now