Click here to monitor SSC
SQLServerCentral is supported by Red Gate Software Ltd.
 
Log in  ::  Register  ::  Not logged in
 
 
 
        
Home       Members    Calendar    Who's On


Add to briefcase 123»»»

How comma separated values are divided into rows? Expand / Collapse
Author
Message
Posted Tuesday, February 4, 2014 11:32 PM
Grasshopper

GrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopper

Group: General Forum Members
Last Login: Friday, March 7, 2014 4:32 AM
Points: 19, Visits: 50
Hi Experts,

I have the input:
rank country city
1 india hyderabad, bangalore, delhi, shimla
2 usa newjersey, newyork, washington, texas
3 uk london, greenland, denmark, italy, spain

expected output:

rank country city
1 india hyderabad
1 india banalore
1 india delhi
1 india shimla similarly for rank 2 and 3


Can anyone help me how to achieve this in SSIS BIDS. What logic should be used and in which componenet.??

Thank you in Advance,
Regards,
Vijayvanamala
Post #1538058
Posted Wednesday, February 5, 2014 12:34 AM


SSChampion

SSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampion

Group: General Forum Members
Last Login: Today @ 1:12 PM
Points: 13,357, Visits: 10,221
You need to use the Unpivot transformation.

How To Use the Unpivot Data Flow Transform in SQL Server Integration Services SSIS

The unpivot however accepts only a fixed number of columns as input though. So you have to make sure the city column is splitted into multiple columns and that this number is fixed.

If this is not possible, you'll have to implement it with a script component.




How to post forum questions.
Need an answer? No, you need a question.
What’s the deal with Excel & SSIS?

Member of LinkedIn. My blog at LessThanDot.

MCSA SQL Server 2012 - MCSE Business Intelligence
Post #1538065
Posted Wednesday, February 5, 2014 8:22 AM


SSC-Dedicated

SSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-Dedicated

Group: General Forum Members
Last Login: Today @ 1:35 PM
Points: 36,787, Visits: 31,246
I wouldn't do this part in SSIS. Post some readily consumable data according to the first link in my signature line below under "Helpful Links" and if I don't get to it, I'm sure that someone else will.

--Jeff Moden
"RBAR is pronounced "ree-bar" and is a "Modenism" for "Row-By-Agonizing-Row".

First step towards the paradigm shift of writing Set Based code:
Stop thinking about what you want to do to a row... think, instead, of what you want to do to a column."

(play on words) "Just because you CAN do something in T-SQL, doesn't mean you SHOULDN'T." --22 Aug 2013

Helpful Links:
How to post code problems
How to post performance problems
Post #1538212
Posted Wednesday, February 5, 2014 8:31 AM


SSChampion

SSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampion

Group: General Forum Members
Last Login: Today @ 1:30 PM
Points: 13,136, Visits: 11,974
I am pretty sure that Jeff was thinking of using the DelimitedSplit8K function for this.

I create the ddl and sample data for you to demonstrate how you should post this in the future.

create table #Something
(
CityRank int,
Country varchar(10),
City varchar(200)
)

insert #Something
select 1, 'india', 'hyderabad, bangalore, delhi, shimla' union all
select 2, 'usa', 'newjersey, newyork, washington, texas' union all
select 3, 'uk', 'london, greenland, denmark, italy, spain'

select CityRank, Country, ltrim(x.Item)
from #Something s
cross apply dbo.DelimitedSplit8K(s.City, ',') x

drop table #Something

You will need to read the article in my signature about splitting strings. In there you will find how to make the DelimitedSplit8K function on your server. Make sure you read and understand the technique here.


_______________________________________________________________

Need help? Help us help you.

Read the article at http://www.sqlservercentral.com/articles/Best+Practices/61537/ for best practices on asking questions.

Need to split a string? Try Jeff Moden's splitter.

Cross Tabs and Pivots, Part 1 – Converting Rows to Columns
Cross Tabs and Pivots, Part 2 - Dynamic Cross Tabs
Understanding and Using APPLY (Part 1)
Understanding and Using APPLY (Part 2)
Post #1538217
Posted Wednesday, February 5, 2014 9:18 AM


SSC-Dedicated

SSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-Dedicated

Group: General Forum Members
Last Login: Today @ 1:35 PM
Points: 36,787, Visits: 31,246
Sean Lange (2/5/2014)
I am pretty sure that Jeff was thinking of using the DelimitedSplit8K function for this.


Correct and well done. Thanks for the cover, Sean.


--Jeff Moden
"RBAR is pronounced "ree-bar" and is a "Modenism" for "Row-By-Agonizing-Row".

First step towards the paradigm shift of writing Set Based code:
Stop thinking about what you want to do to a row... think, instead, of what you want to do to a column."

(play on words) "Just because you CAN do something in T-SQL, doesn't mean you SHOULDN'T." --22 Aug 2013

Helpful Links:
How to post code problems
How to post performance problems
Post #1538234
Posted Wednesday, February 5, 2014 9:22 AM


SSChampion

SSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampion

Group: General Forum Members
Last Login: Today @ 1:30 PM
Points: 13,136, Visits: 11,974
Jeff Moden (2/5/2014)
Sean Lange (2/5/2014)
I am pretty sure that Jeff was thinking of using the DelimitedSplit8K function for this.


Correct and well done. Thanks for the cover, Sean.


My pleasure.


_______________________________________________________________

Need help? Help us help you.

Read the article at http://www.sqlservercentral.com/articles/Best+Practices/61537/ for best practices on asking questions.

Need to split a string? Try Jeff Moden's splitter.

Cross Tabs and Pivots, Part 1 – Converting Rows to Columns
Cross Tabs and Pivots, Part 2 - Dynamic Cross Tabs
Understanding and Using APPLY (Part 1)
Understanding and Using APPLY (Part 2)
Post #1538237
Posted Wednesday, February 5, 2014 11:51 PM


SSCarpal Tunnel

SSCarpal TunnelSSCarpal TunnelSSCarpal TunnelSSCarpal TunnelSSCarpal TunnelSSCarpal TunnelSSCarpal TunnelSSCarpal TunnelSSCarpal Tunnel

Group: General Forum Members
Last Login: Today @ 10:36 AM
Points: 4,985, Visits: 11,681
I tried the UNPIVOT transformation for this problem and after pratting about with it for a while in an attempt to make it work, I have come down soundly on the side of Jeff and Sean - do this outside of SSIS.


Help us to help you. For better, quicker and more-focused answers to your questions, consider following the advice in this link.

When you ask a question (and please do ask a question: "My T-SQL does not work" just doesn't cut it), please provide enough information for us to understand its context.
Post #1538485
Posted Wednesday, February 5, 2014 11:57 PM


SSChampion

SSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampion

Group: General Forum Members
Last Login: Today @ 1:12 PM
Points: 13,357, Visits: 10,221
Phil Parkin (2/5/2014)
I tried the UNPIVOT transformation for this problem and after pratting about with it for a while in an attempt to make it work, I have come down soundly on the side of Jeff and Sean - do this outside of SSIS.


And if you think the UNPIVOT has an unholy GUI, try the PIVOT one for a change...





How to post forum questions.
Need an answer? No, you need a question.
What’s the deal with Excel & SSIS?

Member of LinkedIn. My blog at LessThanDot.

MCSA SQL Server 2012 - MCSE Business Intelligence
Post #1538487
Posted Thursday, February 6, 2014 1:11 AM
SSC-Addicted

SSC-AddictedSSC-AddictedSSC-AddictedSSC-AddictedSSC-AddictedSSC-AddictedSSC-AddictedSSC-Addicted

Group: General Forum Members
Last Login: Today @ 5:50 AM
Points: 470, Visits: 476
Would it be ok for you to use recursive CTE ? ..something like

create table #Something
(
CityRank int,
Country varchar(10),
City varchar(200)
)

insert #Something
select 1, 'india', 'hyderabad, bangalore, delhi, shimla' union all
select 2, 'usa', 'newjersey, newyork, washington, texas' union all
select 3, 'uk', 'london, greenland, denmark, italy, spain'

--SELECT * FROM #Something

;WITH CTE AS
(
SELECT CityRank,Country,City,SUBSTRING(CITY,1,CHARINDEX(',',CITY,1)-1) AS C_CITY,
SUBSTRING(CITY,CHARINDEX(',',CITY,1)+1,LEN(CITY)) AS C_STR FROM #Something
UNION ALL
SELECT CityRank,Country,City,LTRIM(RTRIM(SUBSTRING(C_STR,1,CASE
WHEN CHARINDEX(',',C_STR,1) = 0 THEN LEN(C_STR)
ELSE CHARINDEX(',',C_STR,1) - 1
END ))),
SUBSTRING(C_STR,CASE
WHEN CHARINDEX(',',C_STR,1) = 0 THEN NULL
ELSE CHARINDEX(',',C_STR,1)+1
END,CASE
WHEN LEN(C_STR) = 0 THEN 1
ELSE LEN(C_STR)
END) FROM CTE
WHERE CHARINDEX(',',C_STR,1) >= 0 --OR
)
SELECT CityRank,Country,C_CITY AS CITY FROM CTE ORDER BY CityRank

I'm sorry for the formatting/colors ..as this is my first post and do not know how to post with SQL formatting being intact

Post #1538501
Posted Thursday, February 6, 2014 1:28 AM


SSCarpal Tunnel

SSCarpal TunnelSSCarpal TunnelSSCarpal TunnelSSCarpal TunnelSSCarpal TunnelSSCarpal TunnelSSCarpal TunnelSSCarpal TunnelSSCarpal Tunnel

Group: General Forum Members
Last Login: Today @ 10:36 AM
Points: 4,985, Visits: 11,681
chetan.deshpande001 (2/6/2014)
Would it be ok for you to use recursive CTE ? ..something like

--Edit

I'm sorry for the formatting/colors ..as this is my first post and do not know how to post with SQL formatting being intact



OK ... perhaps, but the proposed Delimited8KSplit solution will outperform it, I'd bet my wife's shoes on that.

Out of interest, how did you get 329 points here without posting anything?

T-SQL formatting: All you need to do is enclose your code with {code="sql"}{/code} tags (use square brackets)



Help us to help you. For better, quicker and more-focused answers to your questions, consider following the advice in this link.

When you ask a question (and please do ask a question: "My T-SQL does not work" just doesn't cut it), please provide enough information for us to understand its context.
Post #1538506
« Prev Topic | Next Topic »

Add to briefcase 123»»»

Permissions Expand / Collapse