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
|
|
0
|
|
|
|
Reply
|
jindan (50)
|
11/10/2007 7:14:50 AM |
|
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
|
|
0
|
|
|
|
Reply
|
J
|
11/10/2007 7:48:53 AM
|
|
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
|
|
0
|
|
|
|
Reply
|
Jindan
|
11/10/2007 3:18:30 PM
|
|
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.
|
|
0
|
|
|
|
Reply
|
Paul
|
11/10/2007 8:51:06 PM
|
|
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
|
|
0
|
|
|
|
Reply
|
J
|
11/10/2007 9:19:50 PM
|
|
|
4 Replies
1070 Views
(page loaded in 0.046 seconds)
Similiar Articles: Split full name in to first, middle, and last name - comp ...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... common email domain suffix - comp.mail.miscnamed common and subprogram names - comp.lang.fortran common email ... Split full name in to first, middle, and last name - comp ... common email domain suffix - comp.mail.misc ... Split Nsf File - comp.groupware.lotus-notes.adminSplit full name in to first, middle, and last name - comp ... Split Nsf File - comp.groupware.lotus ... named common and subprogram names - comp.lang.fortran Split Nsf File ... named common and subprogram names - comp.lang.fortranSplit full name in to first, middle, and last name - comp ... named common and subprogram names - comp.lang.fortran Splitting Names - comp.databases.filemaker named common ... tar terminates unexpectedly when piped to dd ? - comp.os.linux ...Split full name in to first, middle, and last name - comp ... tar terminates unexpectedly when piped to dd ? - comp.os.linux ... One "v" will make tar write the name of ... Insert into Y select * from X - autoincrement fields - comp ...Split full name in to first, middle, and last name - comp ..... with some Google job, I managed to do: select left ... Also does the above syntax creates the new field in ... convert integer to string - comp.lang.perl.misc... calculated field, which is a list of names ... joe|male|etc"; my ($id, $name, $gender) = split ... unless /\w/; chomp; my ($changed, $last, $first, $middle ... Newbie Question: delete all non alphanumeric characters - comp ...Remove last lines - comp.unix.shell Remove first and last 3 ... AWK question - split string into ... Make "ps" list FULL zone name - comp.unix.solaris to see zone names wider ... Line drawing with Bresenham more than one pixel thick - comp.lang ...The first part should be readable ... o# You could split this rectangle into three parts, an upper triangle, a middle parallelogram ... memory] - comp.lang.asm.x86 The last ... How to exec "login" from the lowest level "shell" - comp.unix ...... fds available and your first 'open' got the lowest ... question - split string into ... Archive-name: cdrom/cd-recordable/part1 Posting-Frequency: monthly Last-modified: 2008 ... Trying to Split First initial, middle initial, and Last name ...Trying to Split First initial, middle initial, and Last name/Suffix ... would be the Full name(ex: J H williams III). In column B, the first an ... data needs to be split into ... First And Last Names... to split full names into the first and last name ... full name, as described above, and returns an array for four elements -- the Last Name, the First Name, the Middle ... 7/23/2012 5:49:20 PM
|