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


How to join to a comma delimited list


How to join to a comma delimited list

Author
Message
rightontarget
rightontarget
SSCommitted
SSCommitted (1.5K reputation)SSCommitted (1.5K reputation)SSCommitted (1.5K reputation)SSCommitted (1.5K reputation)SSCommitted (1.5K reputation)SSCommitted (1.5K reputation)SSCommitted (1.5K reputation)SSCommitted (1.5K reputation)

Group: General Forum Members
Points: 1541 Visits: 487
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


J Livingston SQL
J Livingston SQL
One Orange Chip
One Orange Chip (27K reputation)One Orange Chip (27K reputation)One Orange Chip (27K reputation)One Orange Chip (27K reputation)One Orange Chip (27K reputation)One Orange Chip (27K reputation)One Orange Chip (27K reputation)One Orange Chip (27K reputation)

Group: General Forum Members
Points: 27188 Visits: 41353
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

Luis Cazares
Luis Cazares
SSC Guru
SSC Guru (95K reputation)SSC Guru (95K reputation)SSC Guru (95K reputation)SSC Guru (95K reputation)SSC Guru (95K reputation)SSC Guru (95K reputation)SSC Guru (95K reputation)SSC Guru (95K reputation)

Group: General Forum Members
Points: 95656 Visits: 21205
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.


Luis C.
General Disclaimer:
Are you seriously taking the advice and code from someone from the internet without testing it? Do you at least understand it? Or can it easily kill your server?


How to post data/code on a forum to get the best help: Option 1 / Option 2
Alan Burstein
Alan Burstein
SSC-Dedicated
SSC-Dedicated (32K reputation)SSC-Dedicated (32K reputation)SSC-Dedicated (32K reputation)SSC-Dedicated (32K reputation)SSC-Dedicated (32K reputation)SSC-Dedicated (32K reputation)SSC-Dedicated (32K reputation)SSC-Dedicated (32K reputation)

Group: General Forum Members
Points: 32484 Visits: 8578
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

-- Alan Burstein


Helpful links:

Best practices for getting help on SQLServerCentral -- Jeff Moden
How to Post Performance Problems -- Gail Shaw

Nasty fast set-based string manipulation functions:
For splitting strings try DelimitedSplit8K or DelimitedSplit8K_LEAD (SQL Server 2012+)
To split strings based on patterns try PatternSplitCM
Need to clean or transform a string? try NGrams, PatExclude8K, PatReplace8K, DigitsOnlyEE, or Translate8K

I cant stress enough the importance of switching from a sequential files mindset to set-based thinking. After you make the switch, you can spend your time tuning and optimizing your queries instead of maintaining lengthy, poor-performing code. -- Itzik Ben-Gan 2001

rightontarget
rightontarget
SSCommitted
SSCommitted (1.5K reputation)SSCommitted (1.5K reputation)SSCommitted (1.5K reputation)SSCommitted (1.5K reputation)SSCommitted (1.5K reputation)SSCommitted (1.5K reputation)SSCommitted (1.5K reputation)SSCommitted (1.5K reputation)

Group: General Forum Members
Points: 1541 Visits: 487
Thank you.
I also found this:
http://stackoverflow.com/questions/13873701/convert-comma-separated-column-value-to-rows
Jeff Moden
Jeff Moden
SSC Guru
SSC Guru (507K reputation)SSC Guru (507K reputation)SSC Guru (507K reputation)SSC Guru (507K reputation)SSC Guru (507K reputation)SSC Guru (507K reputation)SSC Guru (507K reputation)SSC Guru (507K reputation)

Group: General Forum Members
Points: 507949 Visits: 44277
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

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.
If you think its expensive to hire a professional to do the job, wait until you hire an amateur. -- Red Adair

Helpful Links:
How to post code problems
How to post performance problems
Forum FAQs
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