Click to See Complete Forum and Search --> : using substr function


amrigo
12-29-2009, 12:02 PM
Hi

how can i use some mysql function to make an update query, that take the firstname from a field
today i have two collumns : firstname | lastname
say we have :
firstname = jerry lee
lastname = jerry lee

I need to update to make it to be : firstname = jerry
lastname = lee

can it be done with substr ? what is the best way to do it ?

Thank´s in advance

NogDog
12-30-2009, 09:16 PM
Untested:

UPDATE table SET
firstname = IF(LOCATE(' ', firstname) != 0,
SUBSTRING(firstname, 1, LOCATE(' ', firstname) - 1), firstname),
lastname = IF(LOCATE(' ', lastname) != 0,
SUBSTRING(lastname, LOCATE(' ', firstname) + 1))