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 Wanet Telecoms Ltd on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

parse name fields 1

Status
Not open for further replies.

feshangi

MIS
Nov 24, 2004
265
US
I have a field which holds full name like below:

FullName: Joseph M. Jennings, Jr., QKA

M. sometimes can be full middle name like Michael.

Is it possible parse this field and break it down between 5 fields like below?

First: Joseph
Middle: B.
Last: Jennings
Suffix: Jr.
Designation: QKA

Thanks,

Mike
 
The first thing that comes to mind is to find out how many spaces you have in the string. The problem is though, that if you have 3 spaces (4 separate strings) how do you know whether or not there is only a middle name and no suffix, or no middle name and a Jr. at the end.

My personal preference is to split up names and keep a separate one for first, middle, last, suffix, etc. That way you always know what your fields contain, and you can always concatenate them back together, even inside an SQL statement.

Hope that helps,
MG
 
well, you've got commas in there that can split it up by name, suffix, and Designation.

you're going to have to do alistlen() to see if a suffix is present

if not suffix = ""

then you'll have to get the listlen of the first element (of the comma list) using the space as a delimiter (the name). if there are only two elements the middle name = ""

Code:
[b]this is untested[/b]
<!--- seperate name from suffix and designation --->
<cfset fullName = "Joseph M. Jennings, Jr., QKA">
<cfset name = listGetAt(fullName, 1)>
<cfset designation = listGetLast(fullName)>
<cfif listLen(fullName) eq 2>
<cfset suffix = "">
<cfelse>
<cfset suffix = listGetAt(fullName, 2)>
</cfif>
<!--- sepperate first middle and last from name --->
<cfset first = listGetAt(name,1, " ")>
<cfset last = listLast(name, " ")
<cfif listLen(name) eq 2>
<cfset middle = "">
<cfelse>
<cfset middle = listGetAt(name, 2, " ")>
</cfif>

the output
Code:
<cfoutput>
First: #first#<br>
Middle: #middle#<br>
Last: #last#<br>
Suffix: #suffix#<br>
Designation: #designation#

A common mistake that people make when trying to design something completely foolproof is to underestimate the ingenuity of complete fools.
-Douglas Adams (1952-2001)
 
Bombboy said it was untested.. here's a few revisions.. but its still his code...

Code:
<!--- seperate name from suffix and designation --->
<cfset fullName = "Joseph M. Jennings, Jr., QKA">
<cfset name = listFirst(fullName)>
<cfset designation = listLast(fullName)>
<cfif listLen(fullName) eq 1>
  <cfset suffix = "">
  [red]<cfset designation = "">
<cfelseif listlen(fullName) eq 2>
  <cfset suffix = "">
  <cfset designation = listlast(fullname)>[/red]
<cfelse>
  <cfset suffix = listGetAt(fullName, 2)>
</cfif>
<!--- sepperate first middle and last from name --->
<cfset first = listGetAt(name,1, " ")>
<cfset last = listLast(name, " ")
[blue]<cfif listLen(name) eq 2>
  <cfset middle = "">
<cfelse>
  <cfset middle = listGetAt(name, 2, " ")>
</cfif>[/blue]

The major change is highlighted in red.. It should catch a lack of designations..

One thing though, is that this is not perfect, not unless all your names are identical syntax... But of course, not everyone's a jr/sr/etc...

The blue part is unchanged, I just think he did a good job here.. that's a smart if statement.

ALFII.com
---------------------
If this post answered or helped to answer your question, please reply with such so that forum members with a similar question will know to use this advice.
 
Bombboy said it was untested.. here's a few revisions.. but its still his code...

Code:
<!--- seperate name from suffix and designation --->
<cfset fullName = "Joseph M. Jennings, Jr., QKA">
<cfset name = listFirst(fullName)>
<cfset designation = listLast(fullName)>
<cfif listLen(fullName) eq 1>
  <cfset suffix = "">
  [red]<cfset designation = "">
<cfelseif listlen(fullName) eq 2>
  <cfset suffix = "">
  <cfset designation = listlast(fullname)>[/red]
<cfelse>
  <cfset suffix = listGetAt(fullName, 2)>
</cfif>
<!--- sepperate first middle and last from name --->
<cfset first = listGetAt(name,1, " ")>
<cfset last = listLast(name, " ")
[blue]<cfif listLen(fullname) eq 2>
  <cfset middle = "">
<cfelse>
  <cfset middle = listGetAt(name, 2, " ")>
</cfif>[/blue]

The major change is highlighted in red.. It should catch a lack of designations..

One thing though, is that this is not perfect, not unless all your names are identical syntax... But of course, not everyone's a jr/sr/etc...

The blue part is unchanged, I just think he did a good job here.. that's a smart if statement.

And this is also untested.

ALFII.com
---------------------
If this post answered or helped to answer your question, please reply with such so that forum members with a similar question will know to use this advice.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top