Skip to main content
cancel
Showing results for 
Search instead for 
Did you mean: 
Not applicable

Convert string (comma separated) to multiple columns

Dont ask me why but I have some values stored as comma separated values(CSV) in a column.

I am basically trying to make a loadscript that simulates a excel CSV import, where each of the delimited values end up in a separate column.

Below is an example of two rows in one column:

6000000, , '6006627, '6005599, '6005203, '6003813, '6105008001, '1050006765

, '6002000, '6002092, '6002012, '6006995, '6007703, '6001080, '6206001175

The output that i am looking for is:

Header 1Header 2Header 3Header 4Header 5Header 6Header 7Header 8Header 9
6000000'6003000'6005599'6005203'6003813'6105008001'1050006765
6000000'6002092'6002012'6006995'6007703'6001080'6206001175

Please note that there might be more than 7 delimited values and as you can see the length of each value also varies 😕

Appreciate any help!

Best Regards,

Anders

1 Solution

Accepted Solutions
swuehl
MVP
MVP

Maybe like this:

INPUT:

LOAD * INLINE [

Field

6000000, , '6006627, '6005599, '6005203, '6003813, '6105008001, '1050006765

, '6002000, '6002092, '6002012, '6006995, '6007703, '6001080, '6206001175

] (delimiter is '|';

RESULT:

LOAD *

From_Field (INPUT,Field)

(txt, utf8, explicit labels, delimiter is ',', no quotes);

View solution in original post

4 Replies
swuehl
MVP
MVP

Maybe like this:

INPUT:

LOAD * INLINE [

Field

6000000, , '6006627, '6005599, '6005203, '6003813, '6105008001, '1050006765

, '6002000, '6002092, '6002012, '6006995, '6007703, '6001080, '6206001175

] (delimiter is '|';

RESULT:

LOAD *

From_Field (INPUT,Field)

(txt, utf8, explicit labels, delimiter is ',', no quotes);

Not applicable
Author

Thank you for the swift reply swuehl!

I found a solution last night before i saw your comment.

Basically i just spammed the following until i made sure i had more columns than delimited string variables (then i deleted the surplus) - it worked like a charm.

select

STRTOK(Field,',',1) as Field_1,

STRTOK(Field,',',2) as Field_2,

STRTOK(Field,',',3) as Field_3,

STRTOK(Field,',',4) as Field_4,

.....

from xx

swuehl
MVP
MVP

Yes, you can do similar using the QV function SubField() in a LOAD statement.

But you would need to know the max amount of possible new fields ahead. Probably you know it then.

Not applicable
Author

Yeah, i had some difficulties with the SubField. I'm not sure if it was because the number of delimited variables was unknown Luckily i got it fixed in the sql statement