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
 SQL Server 2005 Forums
 Transact-SQL (2005)
 Split Column Values

Author  Topic 

Guestuser18
Starting Member

34 Posts

Posted - 2009-09-01 : 09:41:08
Hi Guys

I have a column which has data a,b,c,d,e,f
I need to split the data after every comma to put into the other columns in the table so I would have column 1 = a column 2 = b etc etc.
How would I do that?
Thanks

madhivanan
Premature Yak Congratulator

22864 Posts

Posted - 2009-09-01 : 09:57:36
http://sqlblogcasts.com/blogs/madhivanan/archive/2008/09/11/splitting-delimited-data-to-columns-set-based-approach.aspx

Madhivanan

Failing to plan is Planning to fail
Go to Top of Page

cat_jesus
Aged Yak Warrior

547 Posts

Posted - 2009-09-01 : 09:59:21
check this out.

http://www.sqlteam.com/forums/topic.asp?TOPIC_ID=50648&whichpage=2#305425



An infinite universe is the ultimate cartesian product.
Go to Top of Page

madhivanan
Premature Yak Congratulator

22864 Posts

Posted - 2009-09-01 : 10:01:19
quote:
Originally posted by cat_jesus

check this out.

http://www.sqlteam.com/forums/topic.asp?TOPIC_ID=50648&whichpage=2#305425



An infinite universe is the ultimate cartesian product.


That would split into rows and not as columns
See my blog entry

Madhivanan

Failing to plan is Planning to fail
Go to Top of Page

Guestuser18
Starting Member

34 Posts

Posted - 2009-09-01 : 10:02:31
Hi Guys

Thanks for the replies.
However the first link that has been given is banned at my office!!
The second one isnt very clear as to what I should be looking at. I thouhgt it would be a simple thing to do to split a column data up which has commas in it.

thanks
Go to Top of Page

madhivanan
Premature Yak Congratulator

22864 Posts

Posted - 2009-09-01 : 10:06:51
quote:
Originally posted by Guestuser18

Hi Guys

Thanks for the replies.
However the first link that has been given is banned at my office!!
The second one isnt very clear as to what I should be looking at. I thouhgt it would be a simple thing to do to split a column data up which has commas in it.

thanks


Ask your network admistrator to enable the blog

Madhivanan

Failing to plan is Planning to fail
Go to Top of Page

Guestuser18
Starting Member

34 Posts

Posted - 2009-09-01 : 10:13:27
Do you know of any other links that I could use without me going through the process of unblocking a link?
That would be great.
thanks againm guys.
Go to Top of Page

madhivanan
Premature Yak Congratulator

22864 Posts

Posted - 2009-09-01 : 10:32:03
quote:
Originally posted by Guestuser18

Do you know of any other links that I could use without me going through the process of unblocking a link?
That would be great.
thanks againm guys.


Ok. I post the code

CREATE TABLE #test(id int, data varchar(100))

INSERT INTO #test VALUES (1,'This,is,a,test,string')
INSERT INTO #test VALUES (2,'See,if,it,can,be,split,into,many,columns')
DECLARE @pivot varchar(8000)
DECLARE @select varchar(8000)

SELECT
@pivot=coalesce(@pivot+',','')+'[col'+cast(number+1 as varchar(10))+']'
FROM
master..spt_values where type='p' and
number<=(SELECT max(len(data)-len(replace(data,',',''))) FROM #test)

SELECT
@select='
select p.*
from (
select
id,substring(data, start+2, endPos-Start-2) as token,
''col''+cast(row_number() over(partition by id order by start) as varchar(10)) as n
from (
select
id, data, n as start, charindex('','',data,n+2) endPos
from (select number as n from master..spt_values where type=''p'') num
cross join
(
select
id, '','' + data +'','' as data
from
#test
) m
where n < len(data)-1
and substring(data,n+1,1) = '','') as data
) pvt
Pivot ( max(token)for n in ('+@pivot+'))p'

EXEC(@select)

DROP TABLE #test



Madhivanan

Failing to plan is Planning to fail
Go to Top of Page
   

- Advertisement -