Exit
  • Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
  • 한국 커뮤니티
0

How do I convert a full name to firstname lastname

Explorer ,
Sep 21, 2006 Sep 21, 2006
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.
TOPICS
Getting started
1.2K
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines

correct answers 1 Correct answer

Engaged , Sep 21, 2006 Sep 21, 2006
ok, now this should distinguish between an initial in the second position or a last name:

Translate
Engaged ,
Sep 21, 2006 Sep 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
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
Sep 21, 2006 Sep 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.
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Engaged ,
Sep 21, 2006 Sep 21, 2006
ok, now this should distinguish between an initial in the second position or a last name:

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Engaged ,
Sep 21, 2006 Sep 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>
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Engaged ,
Sep 21, 2006 Sep 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...
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
Sep 21, 2006 Sep 21, 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
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Engaged ,
Sep 22, 2006 Sep 22, 2006
LATEST
always happy to help!
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Resources