0

I have a entry in the table that is a string which is delimited by semicolons. Is possible to split the string into separate columns? I've been looking online and at stackoverflow and I couldn't find one that would do the splitting into columns.

The entry in the table looks something like this (anything in brackets [] is not actually in my table. Just there to make things clearer):

sysinfo [column]
miscInfo ; vendor: aaa ; bootr: bbb; revision: ccc; model: ddd [string a]
miscInfo ; vendor: aaa ; bootr: bbb; revision: ccc; model: ddd [string b]
...

There are a little over one million entries with the string that looks like this. Is it possible in mySQL so that the query returns the following

  miscInfo,   Vendor,   Bootr,  Revision ,  Model [columns]
miscInfo_a, vendor_a,  bootr_a, revision_a, model_a
miscInfo_b, vendor_b,  bootr_b, revision_b, model_b
...

for all of the rows in the table, where the comma indicates a new column?

Edit:

Here's some input and output as Bohemian requested.

sysinfo [column]
Modem <<HW_REV: 04; VENDOR: Arris ; BOOTR: 6.xx; SW_REV: 5.2.xxC; MODEL: TM602G>>

<<HW_REV: 1; VENDOR: Motorola ; BOOTR: 216; SW_REV: 2.4.1.5; MODEL: SB5101>>

Thomson DOCSIS Cable Modem <<HW_REV: 4.0; VENDOR: Thomson; BOOTR: 2.1.6d; SW_REV: ST52.01.02; MODEL: DCM425>>

Some can be longer entries but they all have similar format. Here is what I would like the output to be:

miscInfo, vendor, bootr, revision, model [columns]
    04,   Arris,  6.xx, 5.2.xxC, TM602G
    1, Motorola, 216, 2.4.1.5, SB5101
  4.0, Thomson, 2.1.6d, ST52.01.02, DCM425

2 Answers2

0

Please take a look at how I've split my coordinates column into 2 lat/lng columns:

UPDATE shops_locations L 
LEFT JOIN shops_locations L2 ON L2.id = L.id
SET L.coord_lat = SUBSTRING(L2.coordinates, 1, LOCATE('|', L2.coordinates) - 1), 
L.coord_lng = SUBSTRING(L2.coordinates, LOCATE('|', L2.coordinates) + 1)

In overall I followed UPDATE JOIN advice from here MySQL - UPDATE query based on SELECT Query and STR_SPLIT question here Split value from one field to two

Yes I'm just splitting into 2, and SUBSTRING might not work well for you, but anyway, hope this helps :)

Community
  • 1
  • 1
Brock
  • 1,635
  • 2
  • 18
  • 27
0

You could make use of String functions (particularly substr) in mysql: http://dev.mysql.com/doc/refman/5.0/en/string-functions.html

Ashok
  • 1,902
  • 3
  • 22
  • 30
  • Substr requires start and end index. The problem with this is that the ';' is not always in the same index in the string. For an example see my edit above. The first column should explain what I mean. – Christopher Jul 26 '11 at 16:16