Andrea Nagar
Andrea Nagar

Reputation: 1263

Splitting a field into two fields in mySQL

I have a field that contains the fullname of a user (Fullname). I'd need to split it into two other fields FirstName and LastName. Ideally with one query I'd like to be able to populate the FirstName and LastName columns from the value contained in the Fullname. Thanks.

Upvotes: 1

Views: 1368

Answers (1)

Oscar Pérez
Oscar Pérez

Reputation: 4397

You could try (if firstname and lastname are sepparated by a whitespace):

update table_name
   set FirstName=substr(FullName,1,locate(' ',FullName)),
       LastName=substr(FullName,locate(' ',FullName)+1);

Upvotes: 2

Related Questions