Split full name in to first, middle, and last name

This is a discussion on Split full name in to first, middle, and last name within the MySQL Database forums, part of the Database Forums category; Hello group! The full name is stored in the "title" in "mytable", with some Google job, ...


Go Back   Usenet Forums > Database Forums > MySQL Database

FAQ Members List Calendar Search Today's Posts Mark Forums Read
  #1 (permalink)  
Old 11-10-2007
Jindan Zhou
 
Posts: n/a
Default Split full name in to first, middle, and last name

Hello group!
The full name is stored in the "title" in "mytable", with some Google
job, I managed to do:

select left(title,InStr(title,' ')) AS fname,
right(title,(Length(title) -InStr(title,' '))) as lname
from mytable;

This gives me perfect result for first name, but many of the names is
the form "John L. Smith", so I got "L. Smith" for last name which is
no good.

How do I get three new fields that contains first, middle, and last
name? Also does the above syntax creates the new field in the table or
that only displays the query result?

I will need to create the corresponding result in three fields.

Thanks for help!

Jindan

Reply With Quote
  #2 (permalink)  
Old 11-10-2007
J.O. Aho
 
Posts: n/a
Default Re: Split full name in to first, middle, and last name

Jindan Zhou wrote:
> Hello group!
> The full name is stored in the "title" in "mytable", with some Google
> job, I managed to do:
>
> select left(title,InStr(title,' ')) AS fname,
> right(title,(Length(title) -InStr(title,' '))) as lname
> from mytable;
>
> This gives me perfect result for first name, but many of the names is
> the form "John L. Smith", so I got "L. Smith" for last name which is
> no good.


SELECT SUBSTRING_INDEX(title, ' ', 1) AS fname, SUBSTRING_INDEX(title, ' ',
-1) as lname FROM mytable;

This assumes you haven't stored more information in the column.

For three columns:

SELECT SUBSTRING_INDEX(title, ' ', 1) AS fname,
SUBSTRING_INDEX(SUBSTRING_INDEX(title,' ', 2), ' ',-1) AS mname,
SUBSTRING_INDEX(title, ' ', -1) as lname FROM mytable;

There may be faster and better queries.


> How do I get three new fields that contains first, middle, and last
> name? Also does the above syntax creates the new field in the table or
> that only displays the query result?


The things you do in the select statement will only give you a result, never
create any new columns. Keep in mind this is slower than having separated
columns for first/last name.




--

//Aho
Reply With Quote
  #3 (permalink)  
Old 11-10-2007
Jindan Zhou
 
Posts: n/a
Default Re: Split full name in to first, middle, and last name

On Nov 10, 1:48 am, "J.O. Aho" <u...@example.net> wrote:
> Jindan Zhou wrote:
> > Hello group!
> > The full name is stored in the "title" in "mytable", with some Google
> > job, I managed to do:

>
> > select left(title,InStr(title,' ')) AS fname,
> > right(title,(Length(title) -InStr(title,' '))) as lname
> > from mytable;

>
> > This gives me perfect result for first name, but many of the names is
> > the form "John L. Smith", so I got "L. Smith" for last name which is
> > no good.

>
> SELECT SUBSTRING_INDEX(title, ' ', 1) AS fname, SUBSTRING_INDEX(title, ' ',
> -1) as lname FROM mytable;
>
> This assumes you haven't stored more information in the column.
>
> For three columns:
>
> SELECT SUBSTRING_INDEX(title, ' ', 1) AS fname,
> SUBSTRING_INDEX(SUBSTRING_INDEX(title,' ', 2), ' ',-1) AS mname,
> SUBSTRING_INDEX(title, ' ', -1) as lname FROM mytable;
>
> There may be faster and better queries.
>
> > How do I get three new fields that contains first, middle, and last
> > name? Also does the above syntax creates the new field in the table or
> > that only displays the query result?

>
> The things you do in the select statement will only give you a result, never
> create any new columns. Keep in mind this is slower than having separated
> columns for first/last name.
>
> --
>
> //Aho


Thank you! That gives much better result!

How do I create new column to store the them then? Since at the onset
of the design, I just used one column for the full name, now I do
want separate columns for first/middle/last name. Eventually I will
delete the full name column.

Jindan

Reply With Quote
  #4 (permalink)  
Old 11-10-2007
Paul Lautman
 
Posts: n/a
Default Re: Split full name in to first, middle, and last name

Jindan Zhou wrote:
> On Nov 10, 1:48 am, "J.O. Aho" <u...@example.net> wrote:
>> Jindan Zhou wrote:
>>> Hello group!
>>> The full name is stored in the "title" in "mytable", with some
>>> Google job, I managed to do:

>>
>>> select left(title,InStr(title,' ')) AS fname,
>>> right(title,(Length(title) -InStr(title,' '))) as lname
>>> from mytable;

>>
>>> This gives me perfect result for first name, but many of the names
>>> is the form "John L. Smith", so I got "L. Smith" for last name
>>> which is no good.

>>
>> SELECT SUBSTRING_INDEX(title, ' ', 1) AS fname,
>> SUBSTRING_INDEX(title, ' ', -1) as lname FROM mytable;
>>
>> This assumes you haven't stored more information in the column.
>>
>> For three columns:
>>
>> SELECT SUBSTRING_INDEX(title, ' ', 1) AS fname,
>> SUBSTRING_INDEX(SUBSTRING_INDEX(title,' ', 2), ' ',-1) AS mname,
>> SUBSTRING_INDEX(title, ' ', -1) as lname FROM mytable;
>>
>> There may be faster and better queries.
>>
>>> How do I get three new fields that contains first, middle, and last
>>> name? Also does the above syntax creates the new field in the table
>>> or that only displays the query result?

>>
>> The things you do in the select statement will only give you a
>> result, never create any new columns. Keep in mind this is slower
>> than having separated columns for first/last name.
>>
>> --
>>
>> //Aho

>
> Thank you! That gives much better result!
>
> How do I create new column to store the them then? Since at the onset
> of the design, I just used one column for the full name, now I do
> want separate columns for first/middle/last name. Eventually I will
> delete the full name column.
>
> Jindan


Use an UPDATE clause, updating the new fields with the resuts of each of the
functions.


Reply With Quote
  #5 (permalink)  
Old 11-10-2007
J.O. Aho
 
Posts: n/a
Default Re: Split full name in to first, middle, and last name

Jindan Zhou wrote:

> How do I create new column to store the them then? Since at the onset
> of the design, I just used one column for the full name, now I do
> want separate columns for first/middle/last name. Eventually I will
> delete the full name column.


ALTER TABLE
http://dev.mysql.com/doc/refman/5.0/en/alter-table.html

ALTER TABLE mytable ADD COLUMN fname VARCHAR(40);
ALTER TABLE mytable ADD COLUMN mname VARCHAR(40);
ALTER TABLE mytable ADD COLUMN lname VARCHAR(40);

UPDATE mytable SET fname=SUBSTRING_INDEX(title, ' ', 1),
mname=SUBSTRING_INDEX(SUBSTRING_INDEX(title,' ', 2), ' ',-1),
lname=SUBSTRING_INDEX(title, ' ', -1);

ALTER TABLE mytable DROP COLUMN title


Of course before you do something like this, make a backup of the table, in
case something goes wrong, you can at least restore it.


--

//Aho
Reply With Quote
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are Off
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



All times are GMT +1. The time now is 09:54 AM.


Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO 3.0.0