June 16, 2014 at 2:09 pm
Hi all,
I have a table:
id pallet color
1 a red,blue
2 b yellow
and function that will split colors:
declare @colors varchar(100) = 'red,blue'
select * from SeparateValues (@colors,',')
will return
red
blue
Question: how do i join to show:
pallet color
a red
a blue
b yellow
June 16, 2014 at 2:17 pm
does your function return id or pallet?...this would possibly provide a "join"
maybe better if you posted create table/insert sample data scripts and your expected results...am sure this will result is a tried and tested answer for you.
________________________________________________________________
you can lead a user to data....but you cannot make them think
and remember....every day is a school day
June 16, 2014 at 2:31 pm
You might be looking for something like this:
http://www.sqlservercentral.com/articles/comma+separated+list/71700/
Read the article and ask any questions that you might have.
June 16, 2014 at 3:17 pm
rightontarget (6/16/2014)
Hi all,I have a table:
id pallet color
1 a red,blue
2 b yellow
and function that will split colors:
declare @colors varchar(100) = 'red,blue'
select * from SeparateValues (@colors,',')
will return
red
blue
Question: how do i join to show:
pallet color
a red
a blue
b yellow
I think you are looking for this:
DECLARE @YourTable TABLE
(
id int primary key,
pallet char(1) not null,
color varchar(100) not null
)
INSERT @YourTable (id, pallet, color)
VALUES(1, 'a', 'red,blue'),(2, 'b', 'yellow');
SELECT pallet, Item
FROM @YourTable t
CROSS APPLY SeparateValues(color,',')
Edit: Added SQL Code tag thingee around my solution
-- Itzik Ben-Gan 2001
June 16, 2014 at 3:49 pm
Thank you.
I also found this:
http://stackoverflow.com/questions/13873701/convert-comma-separated-column-value-to-rows
June 16, 2014 at 8:26 pm
rightontarget (6/16/2014)
Thank you.I also found this:
http://stackoverflow.com/questions/13873701/convert-comma-separated-column-value-to-rows
That uses both an XML conversion and concatenation. That also makes it comparatively slow. Here's a comparison chart for 1,000 CSVs. Consider using the DelimitedSplit8K function found in the "resources" link at the bottom of the following article. The Black line is the new DelimitedSplit8K (and it's actually 10-20% faster than that thanks to an additional change).
--Jeff Moden
Change is inevitable... Change for the better is not.
Viewing 6 posts - 1 through 5 (of 5 total)
You must be logged in to reply to this topic. Login to reply