Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 Splitting a single value into separate records

Author  Topic 

evanburen
Posting Yak Master

167 Posts

Posted - 2009-09-04 : 10:22:25
I have no idea where to start on something like this. I inherited a database that needs some help.


I have a fieldname Fields.ConcatenatedLabels which contains values like these where the product names have been placed into a single field delimitted by a pipe character.

Board Analyst
Board Analyst | Chartmaker
Board Analyst | Chartmaker | GIST
Networking Tool | GIST

I want to split these values out into a new table into separate records.

The current table named Fields looks like this

id_field (pk)
ConcatenatedLabels (sample data above)

The new table named Bridge should look like this

id (pk)
id_field
id_product

1 10 1
2 10 5
3 17 1
4 17 2
5 17 3
6 18 1
7 18 3
8 19 1
9 19 3

The id_products are

Board Analyst = 1
Chartmaker = 2
GIST = 3
Interlocks = 4
Networking Tool = 5

Thanks

TG
Master Smack Fu Yak Hacker

6065 Posts

Posted - 2009-09-04 : 10:40:52
The idea would be to create a user defined function (table valued UDF) which will parse a delimited value into separate rows. Then use the udf as a table JOINed to your [fields] table and [id_products] table to return your desired results for your new table.

Search for split functions here for examples.

Be One with the Optimizer
TG
Go to Top of Page

TG
Master Smack Fu Yak Hacker

6065 Posts

Posted - 2009-09-04 : 11:26:42
Here is a simple example to give you the idea. You will likely want to do some research and improve on this split function.

use tempdb
go
create table [fields] (id int, field varchar(2000))
go
create table [products] (productID int, productDesc varchar(200))
go

insert [products]
select 1, 'Board Analyst' union all
select 2, 'Chartmaker' union all
select 3, 'GIST' union all
select 4, 'Interlocks' union all
select 5, 'Networking'

insert [Fields]
select 1, 'Board Analyst' union all
select 2, 'Board Analyst | Chartmaker' union all
select 3, 'Board Analyst | Chartmaker | GIST' union all
select 4, 'Networking Tool | GIST'

go

create function dbo.splitFields (@f varchar(2000))
returns @t table (fld varchar(200))
as
begin
declare @i int
set @f = '|' + replace(replace(@f,' |', '|'),'| ', '|') + '|'
select @i = charindex('|', @f, 1)
while @i > 0 and @i < len(@f)
begin
insert @t
select substring(@f, @i+1, charindex('|', @f, @i+1)-@i-1 )
set @i = charindex('|', @f, @i+1)
end
return
end
go

select row_number() over (order by fs.id, p.productid) as rowID
,fs.id, p.productid
from [fields] fs
cross apply ( select fld from dbo.splitFields(fs.field) ) f
inner join [products] p
on p.productDesc = f.fld


go
drop function dbo.splitFields
drop table [fields]
drop table [products]
go

OUTPUT:
rowID id productid
-------------------- ----------- -----------
1 1 1
2 2 1
3 2 2
4 3 1
5 3 2
6 3 3
7 4 3


Be One with the Optimizer
TG
Go to Top of Page

evanburen
Posting Yak Master

167 Posts

Posted - 2009-09-04 : 12:52:09
This is great. Thanks very much.
Go to Top of Page
   

- Advertisement -