SQL Clone
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


How to Convert Semi colon Separated Values into Column


How to Convert Semi colon Separated Values into Column

Author
Message
joshtheflame
joshtheflame
Old Hand
Old Hand (305 reputation)Old Hand (305 reputation)Old Hand (305 reputation)Old Hand (305 reputation)Old Hand (305 reputation)Old Hand (305 reputation)Old Hand (305 reputation)Old Hand (305 reputation)

Group: General Forum Members
Points: 305 Visits: 203
I am working on a integration project where I am receiving three string parameters ItemCode ItemName Amount

and values will be in this format

ItemCod Parameter values
T1;T2;T3;

ItemName Parameter values
Pencil Box;Eraser;Mouse Pad;

Amount Paramter values
1900;2000;8900;

Now I would like to have a procedure in which i would receive these parameters and the query will convert this as column and records. I am not a SQLServer guy so somebody proposed a solution but it is limited to two records only where my requirement is as many records depends on the number of semi colon separated strings. The defined structure is that all parameters will have equal number of values.

here is the solution somebody gave it to me.


INSERT INTO t
(cod, name)
VALUES
('T1;T2;T3;T4;T5;',
'Pencil Box;Eraser;Board Marker;Trimmer;Ball Point;');


SELECT
CAST('<r>'+REPLACE(cod,';','</r><r>')+'</r>' AS XML).query('/r[1]').value('.','varchar(256)') cod,
CAST('<r>'+REPLACE(name,';','</r><r>')+'</r>' AS XML).query('/r[1]').value('.','varchar(256)') name

INTO #tmpTable FROM t

insert INTO #tmpTable
SELECT
CAST('<r>'+REPLACE(cod,';','</r><r>')+'</r>' AS XML).query('/r[2]').value('.','varchar(256)') cod,
CAST('<r>'+REPLACE(name,';','</r><r>')+'</r>' AS XML).query('/r[2]').value('.','varchar(256)') name
FROM t;

select * from #tmpTable



Here is the helper script to create required table so it wont waste your valuable time


CREATE TABLE [dbo].[t](
[cod] [varchar](350) NULL,
[name] [varchar](300) NULL
) ON [PRIMARY]

GO


Bhuvnesh
Bhuvnesh
SSChampion
SSChampion (13K reputation)SSChampion (13K reputation)SSChampion (13K reputation)SSChampion (13K reputation)SSChampion (13K reputation)SSChampion (13K reputation)SSChampion (13K reputation)SSChampion (13K reputation)

Group: General Forum Members
Points: 13510 Visits: 4077
post the expected output based on your above data's example

-------Bhuvnesh----------
I work only to learn Sql Server...though my company pays me for getting their stuff done;-)
Hotblack
Hotblack
SSC-Enthusiastic
SSC-Enthusiastic (178 reputation)SSC-Enthusiastic (178 reputation)SSC-Enthusiastic (178 reputation)SSC-Enthusiastic (178 reputation)SSC-Enthusiastic (178 reputation)SSC-Enthusiastic (178 reputation)SSC-Enthusiastic (178 reputation)SSC-Enthusiastic (178 reputation)

Group: General Forum Members
Points: 178 Visits: 113
You could use a recursive cte to split the strings:

WITH rep AS
(
SELECT
cod
,';' delim
,name
from t

UNION ALL

SELECT
LEFT(cod, CHARINDEX(delim, cod, 1) - 1)
,delim
,LEFT(name, CHARINDEX(delim, name, 1) - 1)
FROM rep
WHERE (CHARINDEX(delim, cod, 1) > 0)

UNION ALL

SELECT
RIGHT(cod, LEN(cod) - CHARINDEX(delim, cod, 1))
,delim
,RIGHT(name, LEN(name) - CHARINDEX(delim, name, 1))
FROM rep
WHERE (CHARINDEX(delim, cod, 1) > 0)
)
SELECT
cod
,name
FROM rep
WHERE (CHARINDEX(delim, cod, 1) = 0)
AND LEN(cod)>0
OPTION (MAXRECURSION 0);





ChrisM@Work
ChrisM@Work
SSC-Forever
SSC-Forever (42K reputation)SSC-Forever (42K reputation)SSC-Forever (42K reputation)SSC-Forever (42K reputation)SSC-Forever (42K reputation)SSC-Forever (42K reputation)SSC-Forever (42K reputation)SSC-Forever (42K reputation)

Group: General Forum Members
Points: 42132 Visits: 20009
DECLARE @ItemCod VARCHAR(8000) = 'T1;T2;T3;'
DECLARE @ItemName VARCHAR(8000) = 'Pencil Box;Eraser;Mouse Pad;'
DECLARE @Amount VARCHAR(8000) = '1900;2000;8900;'

SELECT
c.ItemNumber,
ItemCod = c.Item,
ItemName = n.Item,
Amount = a.Item
FROM dbo.DelimitedSplit8K(@ItemCod,';') c
INNER JOIN dbo.DelimitedSplit8K(@ItemName,';') n ON n.ItemNumber = c.ItemNumber
INNER JOIN dbo.DelimitedSplit8K(@Amount,';') a ON a.ItemNumber = c.ItemNumber
WHERE c.Item <> ''

-- Results
ItemNumber ItemCod ItemName Amount
1 T1 Pencil Box 1900
2 T2 Eraser 2000
3 T3 Mouse Pad 8900



The function DelimitedSplit8K is discussed in this article.

“Write the query the simplest way. If through testing it becomes clear that the performance is inadequate, consider alternative query forms.” - Gail Shaw

For fast, accurate and documented assistance in answering your questions, please read this article.
Understanding and using APPLY, (I) and (II) Paul White
Hidden RBAR: Triangular Joins / The "Numbers" or "Tally" Table: What it is and how it replaces a loop Jeff Moden
Exploring Recursive CTEs by Example Dwain Camps
Arjun Sivadasan
Arjun Sivadasan
Ten Centuries
Ten Centuries (1.3K reputation)Ten Centuries (1.3K reputation)Ten Centuries (1.3K reputation)Ten Centuries (1.3K reputation)Ten Centuries (1.3K reputation)Ten Centuries (1.3K reputation)Ten Centuries (1.3K reputation)Ten Centuries (1.3K reputation)

Group: General Forum Members
Points: 1338 Visits: 976
Life will be much easier if you pass that as an XML parameter.

DECLARE @x XML = 
'
<Items>
<Item>
<ItemCode>T1</ItemCode>
<ItemName>Pencil Box</ItemName>
<Amount>1900</Amount>
</Item>
<Item>
<ItemCode>T2</ItemCode>
<ItemName>Eraser</ItemName>
<Amount>2000</Amount>
</Item>
<Item>
<ItemCode>T3</ItemCode>
<ItemName>Mouse Pad</ItemName>
<Amount>8900</Amount>
</Item>
</Items>
';


SELECT
i.value('(ItemCode)[1]','varchar(50)'),
i.value('(ItemName)[1]','varchar(50)'),
i.value('(Amount)[1]','numeric')
FROM
@x.nodes('//Items/Item') TAB(i)


joshtheflame
joshtheflame
Old Hand
Old Hand (305 reputation)Old Hand (305 reputation)Old Hand (305 reputation)Old Hand (305 reputation)Old Hand (305 reputation)Old Hand (305 reputation)Old Hand (305 reputation)Old Hand (305 reputation)

Group: General Forum Members
Points: 305 Visits: 203
Chris...hats off to yaaaaaaaaaaaaaaaaa Smile so awesome and superb fast...I simply praised you for the awesome query Smile Wish I could learn SQL like ya Smile


ChrisM@Work (3/7/2013)
DECLARE @ItemCod VARCHAR(8000) = 'T1;T2;T3;'
DECLARE @ItemName VARCHAR(8000) = 'Pencil Box;Eraser;Mouse Pad;'
DECLARE @Amount VARCHAR(8000) = '1900;2000;8900;'

SELECT
c.ItemNumber,
ItemCod = c.Item,
ItemName = n.Item,
Amount = a.Item
FROM dbo.DelimitedSplit8K(@ItemCod,';') c
INNER JOIN dbo.DelimitedSplit8K(@ItemName,';') n ON n.ItemNumber = c.ItemNumber
INNER JOIN dbo.DelimitedSplit8K(@Amount,';') a ON a.ItemNumber = c.ItemNumber
WHERE c.Item <> ''

-- Results
ItemNumber ItemCod ItemName Amount
1 T1 Pencil Box 1900
2 T2 Eraser 2000
3 T3 Mouse Pad 8900



The function DelimitedSplit8K is discussed in this article.

ChrisM@Work
ChrisM@Work
SSC-Forever
SSC-Forever (42K reputation)SSC-Forever (42K reputation)SSC-Forever (42K reputation)SSC-Forever (42K reputation)SSC-Forever (42K reputation)SSC-Forever (42K reputation)SSC-Forever (42K reputation)SSC-Forever (42K reputation)

Group: General Forum Members
Points: 42132 Visits: 20009
Gosh Blush thanks! Hang around here, read the articles...you'll soon pick it up.

“Write the query the simplest way. If through testing it becomes clear that the performance is inadequate, consider alternative query forms.” - Gail Shaw

For fast, accurate and documented assistance in answering your questions, please read this article.
Understanding and using APPLY, (I) and (II) Paul White
Hidden RBAR: Triangular Joins / The "Numbers" or "Tally" Table: What it is and how it replaces a loop Jeff Moden
Exploring Recursive CTEs by Example Dwain Camps
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search