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 column in to several columsn

Author  Topic 

shiyam198
Yak Posting Veteran

94 Posts

Posted - 2011-05-27 : 16:27:51
I tried this problem my self and I could not pass the first data.

So, I have column with data such as text01,test02,test02,test03. the highest number of data on the column is 13 data.

I have created 13 more columns on the table to insert that data.
I am able to spilit the first data using subscrting and charindex and insert the first data to the first column. I am unable to do the consequent data. I have been grinding my teeth on this for past 2 hours. If someone has a way to acheive this, can you please let me know

Many thanks,
Shiyam



sunitabeck
Master Smack Fu Yak Hacker

5155 Posts

Posted - 2011-05-27 : 17:56:56
You need a way to split the comma-separated string into individual tokens. There are a number of methods to do this, one that I like is the code in Fig. 21 of Jeff Moden's article here: http://www.sqlservercentral.com/articles/Tally+Table/72993/

Copy the code and run it to install the function, and then use that to split the string into tokens and insert into where you need it to be.
Go to Top of Page

jfarrugia
Yak Posting Veteran

55 Posts

Posted - 2011-05-28 : 12:38:34
Try this:

http://www.itexposed.com/forum/general-sql-queries/split-function-in-sql-server/

its a split function which can be reused.. you basically pass the whole string + delimiter as parameters and it will return them in several columns as you desire.

Where software development knowledge meets the reader
Go to Top of Page

madhivanan
Premature Yak Congratulator

22864 Posts

Posted - 2011-05-30 : 06:34:04
Also try this
http://beyondrelational.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

shiyam198
Yak Posting Veteran

94 Posts

Posted - 2011-05-30 : 14:35:13
Thanks a lot all.
Go to Top of Page

Jeff Moden
Aged Yak Warrior

652 Posts

Posted - 2011-06-10 : 00:57:22
quote:
Originally posted by jfarrugia

Try this:

http://www.itexposed.com/forum/general-sql-queries/split-function-in-sql-server/

its a split function which can be reused.. you basically pass the whole string + delimiter as parameters and it will return them in several columns as you desire.

Where software development knowledge meets the reader




My recommendation is to not use splitters that have loops in them. They're pretty slow.


--Jeff Moden
Go to Top of Page
   

- Advertisement -