1

Is there a way to separate a column into two ?

The column its like this

BRAND/PRODUCT

And I wanted to insert them into 2 new columns BRAND and PRODUCT

Thanks

ForeverSJC
  • 312
  • 2
  • 4
  • 13

2 Answers2

8

Use LOCATE to find the /, and SUBSTR to grab the parts of the string before and after it.

UPDATE table
SET brand = SUBSTR(brand_product, 1, LOCATE('/', brand_product)-1),
    product = SUBSTR(brand_product, LOCATE('/', brand_product)+1)
Barmar
  • 741,623
  • 53
  • 500
  • 612
3

Essentially splits the field value. The "1" in the first one gets the first text segment, the "-1" in the second one gets the last text segment.

UPDATE `table` 
  SET `brand`   = SUBSTRING_INDEX(`brand_product`, '/', 1), 
      `product` = SUBSTRING_INDEX(`brand_product`, '/', -1);

Output:

| brand_product | brand | product |
+---------------+-------+---------+
| one/two       | one   | two     |

See: http://dev.mysql.com/doc/refman/5.0/en/string-functions.html#function_substring-index

bloodyKnuckles
  • 11,551
  • 3
  • 29
  • 37