syrkull
syrkull

Reputation: 2344

separate a column into two columns (sql)

I have column with the following information:

Author  varchar(255)    utf8_general_ci

the programmer who designed the website before me used this column to store two things! first, the name of the author in English, then his/her name in Arabic ! so, all the data in that column have the same structure :تووياما أيما (Tooyama Ema) between the parenthesis is the name in English. outside the parentheses the name in Arabic. Is there any way to take the English name and put it in a new column. plus remove the parenthesis? I can use both PHP and SQL, so please can you give me a solution with those two languages? I want the result to look like this :

Author : تووياما أيما Author in English : Tooyama Ema

I am talking about many rows with the same structure. and I want to change the structure in the database.

Upvotes: 1

Views: 132

Answers (2)

quickshiftin
quickshiftin

Reputation: 69581

I'd recommend an db-driven solution, should take less time coding and running than using PHP.

Not sure on the table name, but here is the meat of what you need, change to your liking:

(assuming rough table definition of)

CREATE TABLE Test (
  id int(11) not null auto_increment,
  Author varchar(255),
  primary key (id)
)

An alter table for the new columns:

ALTER TABLE Test ADD COLUMN `arabic_name` varchar(255) AFTER `Author`;
ALTER TABLE Test ADD COLUMN `english_name` varchar(255) AFTER `arabic_name`;

A simple update statement to migrate the data (not the prettiest, but works just fine):

 UPDATE Test
 SET
 arabic_name = SUBSTR(TRIM(Author), 1, LOCATE(' ', TRIM(Author)) - 1),
 english_name = SUBSTR(TRIM(Author), LOCATE('(', TRIM(Author)) + 1, LOCATE(')', TRIM(Author)) -  LOCATE('(', TRIM(Author)) - 1);

Upvotes: 3

Madara's Ghost
Madara's Ghost

Reputation: 174947

$str = "تووياما أيما (Tooyama Ema)";
preg_match("/(.+) \((.+)\)/", $str, $matches);

var_dump($matches);

Upvotes: 3

Related Questions