1

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.

Andrea Nagar
  • 1,233
  • 2
  • 13
  • 23

1 Answers1

2

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);
Oscar Pérez
  • 4,377
  • 1
  • 17
  • 36
  • Or us [`SUBSTRING_INDEX()`](http://dev.mysql.com/doc/en/string-functions.html#function_substring-index). – eggyal Jan 10 '13 at 12:10