Skip to main content
Inspiring
September 21, 2006
Answered

How do I convert a full name to firstname lastname

  • September 21, 2006
  • 5 replies
  • 1121 views
I need to parse some text that I have in a DB. I have over 5000 records to output, so I am hoping something will be effiecient. However, it is a one time process, so it is not that big of a deal.

I have a field 'fullName'. It contains a users full name (as the name implies). I need to output this so that the 'firstName' is in one field and the 'lastName' is in another field. The entries range from 'Jim Smith' to 'John C. Dvorak Jr.' (not really John, but I wanted to keep this interesting).

In the case of John, I need the first name and Middle initial to end up in the 'firstName' field and everything else in the last name field.

I tried to loop over using a space as the delimeter, but that just sepeates everything into parts. I need the middle initial to hang with the first name and anything trailing the last name to remain there.


Any clever thoughts? Help is much appreciated.
This topic has been closed for replies.
Correct answer azadisg
ok, now this should distinguish between an initial in the second position or a last name:

5 replies

Inspiring
September 22, 2006
always happy to help!
roblawAuthor
Inspiring
September 22, 2006
Azadi,

Thank you so much!! This works perfect and fits exactly what we need. You have been a huge help and I hope that other people searching are able to find your answer.

roblaw
Inspiring
September 21, 2006
oh, hmm... my code, in case of John Dvorak Jr., will assign John Dvorak as firstname and Jr. as lastname...

need to add another cfif... give me a couple of minutes...
Inspiring
September 21, 2006
ok, try this:

<cfoutput query="getUsers">
<cfset numelements = listlen(getUsers.fullName, " ")><!--- this will give you number of elements in fullName --->
<cfif numelements is 2>
<cfset firstname = listfirst(getUsers.fullName, " ")><!--- assign first element to firstname --->
<cfset lastname = listlast(getUsers.fullName, " ")><!--- assign last (2nd) element to lastname --->
<cfelseif numelements gte 3><!--- 3 or more elements in fullName --->
<cfset firstname = listgetat(getUsers.fullName, 1, " ") & " " & listgetat(getUsers.fullName, 2, " ")><!--- assign elements 1 and 2 to firstname --->
<cfset lastname = right(getUsers.fullName, len(getUsers.fullName) - len(firstname) - 1)><!--- assign the rest of the string (less the space separating 2nd and 3rd elements [the -1]) to lastname --->
<cfelse><!--- 0 or 1 elements in fullName --->
<cfset firstname = getUsers.fullName>
<cfset lastname = ''>
</cfif>
</cfoutput>
Inspiring
September 21, 2006
you can use listlen() function on each record to deternime how many spaces are in the fullname, and then use cfif/cfelse to separate it into first/last accordingly
roblawAuthor
Inspiring
September 21, 2006
Azadi,

Thanks for the quick reply. Can you assist with a sample of code for each name provided? I am not very familar with parsing lists.

I am assuming that you mean the Cfif will do a,b,c, or d based upon the listlen. If I know that the list is 4 items, what code would I use to set the first item as one output and the last 3 as a separate output?

Thanks much.
azadisgCorrect answer
Inspiring
September 21, 2006
ok, now this should distinguish between an initial in the second position or a last name: