June 3, 2013 at 3:07 pm
I have a table with a column id and a column with a list of foreign ids.
------------------------------------
| ID | PRICE | LIST_OF_FOREIGN_IDS |
------------------------------------
| 3 | 89 | 67,68,69 |
------------------------------------
| 4 | 120 | 45,46 |
------------------------------------
I need a way to get a view like the one below. Any solutions, ideas?
---------------------------
| ID | PRICE | FOREIGN_ID |
---------------------------
| 3 | 89 | 67 |
---------------------------
| 3 | 89 | 68 |
---------------------------
| 3 | 89 | 69 |
---------------------------
| 4 | 120 | 45 |
---------------------------
| 4 | 120 | 46 |
---------------------------
June 3, 2013 at 3:13 pm
That sounds like a work for the fantastic CSV Splitter.
Read the following article: http://www.sqlservercentral.com/articles/Tally+Table/72993/
June 4, 2013 at 7:43 am
I agree with Luis that you need the splitter. I would however recommend going one step further and normalizing this data. You should not be storing delimited values like that in a single column. You could probably use the same structure you have but a row for each value instead all crammed into a single column.
_______________________________________________________________
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 Modens splitter http://www.sqlservercentral.com/articles/Tally+Table/72993/.
Cross Tabs and Pivots, Part 1 – Converting Rows to Columns - http://www.sqlservercentral.com/articles/T-SQL/63681/
Cross Tabs and Pivots, Part 2 - Dynamic Cross Tabs - http://www.sqlservercentral.com/articles/Crosstab/65048/
Understanding and Using APPLY (Part 1) - http://www.sqlservercentral.com/articles/APPLY/69953/
Understanding and Using APPLY (Part 2) - http://www.sqlservercentral.com/articles/APPLY/69954/
June 4, 2013 at 7:46 am
Here is what your query would look like using the DelimitedSplit8K.
create table #MyTable
(
ID int,
Price int,
List varchar(20)
)
insert #MyTable
select 3, 89, '67,68,69' union all
select 4, 120, '45,46'
select t.ID, t.Price, x.Item
from #MyTable t
cross apply dbo.DelimitedSplit8K(List, ',') x
drop table #MyTable
_______________________________________________________________
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 Modens splitter http://www.sqlservercentral.com/articles/Tally+Table/72993/.
Cross Tabs and Pivots, Part 1 – Converting Rows to Columns - http://www.sqlservercentral.com/articles/T-SQL/63681/
Cross Tabs and Pivots, Part 2 - Dynamic Cross Tabs - http://www.sqlservercentral.com/articles/Crosstab/65048/
Understanding and Using APPLY (Part 1) - http://www.sqlservercentral.com/articles/APPLY/69953/
Understanding and Using APPLY (Part 2) - http://www.sqlservercentral.com/articles/APPLY/69954/
Viewing 4 posts - 1 through 3 (of 3 total)
You must be logged in to reply to this topic. Login to reply