Link to home
Start Free TrialLog in
Avatar of H-SC
H-SCFlag for United States of America

asked on

Split one comma delimited string in a column into 100

Hello,
I have a table called tmp_tbl that only has one column called field1.
I populate that column with a single comma delimited string that has approx 100 commas or splits
How can I get this one column to create 100 columns with each piece of delimited data?

I have looked at every possible split function and can't find one that I can get to work.
Thanks in advance!
Avatar of Kent Olsen
Kent Olsen
Flag of United States of America image

Hi H-SC,

How many rows are in the table?
Is this a process that you'll want to run more than once?
Avatar of Deepak Subburajan
Deepak Subburajan

You can achieve this by Dynamic Queries.

Lets assume your table is "mytable" and the one column is "commacolumns"

Declare @buildtable VARCHAR(255)

SELECT @buildtable = 'SELECT ' + REPLACE(commacolumns, ',' , 'VARCHAR(30),') from mytable

SELECT @buildtable = @buildtable + ' INTO newtablename'

EXEC @buildtable

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of Vikas Garg
Vikas Garg
Flag of India image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of H-SC

ASKER

kdo,
Thanks for the reply....
There will be only several rows at any given time in the table.
Yes, this will be ran on a daily basis.
Avatar of H-SC

ASKER

Deepak Subburajan,
I ran your solution and got an error of
"the name 'SELECT .......and then my string of data .... is not a valid identifier"

any ideas?
Avatar of H-SC

ASKER

Vikas Garg,
Can you give an example of the use of the function?
H-SC,

Please read through your question once again. You mentioned "create 100 columns" which means you wanted to split the single column into multiple columns(same/new table). I think you were about to mean "split the single cell into multiple rows". Isn't it?
Daily basis.  Rats.  So much for just copying the data into Excel and letting its tools split the data

Meta-SQL is probably the way to go.  There is already a post steering you that way.

If all of the data is numeric,

  SELECT 'INSERT INTO mytable SELECT ' + columnvalue FROM sometable;

Will generate the SQL to insert the data.  However, it's more complicated if the columns are character types, or if every row doesn't contain the exact number of columns that are defined in the table.
Avatar of H-SC

ASKER

Deepak Subburajan,
Well I need to do the following

field1 in my table looks like this:
10022, test, test122, helloworld, , , test5, helloworld2,

I need field1 to break apart into multiple columns not as rows...
so field1 would now look like:

col1      col2    col3        col4              col5
10022   test    test122   helloworld           .......... and so on.
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial