Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Pull Last Name 3

Status
Not open for further replies.

bernie10

Technical User
Sep 13, 2004
219
US
Hey everyone,
Suppose I have this sample data in the query MyQuery in the field FullName:

John Smith
Julie L. Jones
Josh Jacob Wright

I would like to create a new field to pull out the last name so I can sort by last name. Could somebody suggest the SQL that would do that.

Thanks,
Collen
 
How about something like:
Code:
Surname: Right([Fullname],(Len([Fullname])-InStrRev([Fullname]," ")))
This code takes the right X characters where X is the length of the value - the position number of the last space.

Hope this helps

HarleyQuinn
---------------------------------
Get the most out of Tek-Tips, read FAQ222-2244 before posting.
 
For your small sampling, and expression like:
LastName: Right([FullName],Len([FullName])-InStrRev([FullName]," "))
should work. You just need to hope no one has a space in their last name.

Duane MS Access MVP
[green]Ask a great question, get a great answer.[/green] [red]Ask a vague question, get a vague answer.[/red]
[green]Find out how to get great answers faq219-2884.[/green]
 
Hi Collen,

Sounds like you've got it sorted, but I think you might find this useful when working with FullName columns. I wrote the functions to deal with full names I had in the same format as yours (consider yourself lucky you don't have salutations and suffixes though ;-) )

faq705-6468

Good Luck,

Alex

Ignorance of certain subjects is a great part of wisdom
 
Alex,
Great code! thanks for sharing the link.

Duane MS Access MVP
[green]Ask a great question, get a great answer.[/green] [red]Ask a vague question, get a vague answer.[/red]
[green]Find out how to get great answers faq219-2884.[/green]
 
Anyway, why using Right and Len when a single Mid suffice ?
LastName: Mid([Fullname],1+InStrRev([Fullname]," "))
 
Duane, that is high praise coming from you. Thanks!



Ignorance of certain subjects is a great part of wisdom
 
Could update from PH. I started thinking Right() and was stuck.

Duane MS Access MVP
[green]Ask a great question, get a great answer.[/green] [red]Ask a vague question, get a vague answer.[/red]
[green]Find out how to get great answers faq219-2884.[/green]
 
Curse you PH, now I will need to change that in my modules and update the FAQ's ;-)

Just kidding, have a star for saving me the extra function calls.

Ignorance of certain subjects is a great part of wisdom
 
This works great, Thanks for the Info, I have a question on how to achieve something very similar, accept It is for a two parted First Name.... Such as Jo Ann or Beth Ann....

Or if there is a way to Remove the Last Name and be left with the rest. Because Not all first names are as this some are one word.....

Just curious... Very good post. Thanks
 
a way to Remove the Last Name
Left([Fullname],InStrRev([Fullname]," ")-1)

Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ181-2886
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top