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

Extract last name from name field

Status
Not open for further replies.

skystar70

Technical User
Nov 3, 2006
40
US
Hi. I have v9. I have a name field that is laid out as "FirstName LastName". I do not have two different fields for the first and last name. They are in the same field. Is there a way to create a formula that only displays the LastName? The LastName will always be after the space.

Thanks!
Sky
 
Try this:

whileprintingrecords;
numbervar blank := Instr({table.field," ");
numbervar char := len(trim({table.field));
{table.field}[blank+1 to char]

Howard Hammerman,
Crystal Training and Crystal Material
On-site classes and one-on-one coaching
Low-cost telephone/email support
FREE independent Crystal newsletter
howard@hammerman.com
800-783-2269
 
Or this formula
Code:
@LastName
Local StringVar array  Nameparts := Split({Table.Field});
Nameparts[2]



[profile]

To Paraphrase:"The Help you get is proportional to the Help you give.."
 
Hi. Both formulas worked, but Nameparts was better as I discovered I had some records with multiple spaces in that field. I was able to use the following formula:

Local StringVar array Nameparts := Split({st_code55.description});
if {st_code55.description} startswith ["CTS", "PT", "OT"] then
Nameparts [4] else
Nameparts[2]

The only other thing I want to do in this formula is display "Unassigned" if the field is null. I tried to add this to the formula (see below), but it left the null as blank. Any ideas:

Local StringVar array Nameparts := Split({st_code55.description});
If IsNull({st_code55.description}) then "Unassigned" else
if {st_code55.description} startswith ["CTS", "PT", "OT"] then
Nameparts [4] else
Nameparts[2]

 
I would try adding a 2nd part to the null check using TRIM and using a few more parenthesis for clarity:


Local StringVar array Nameparts := Split({st_code55.description});

If
(
IsNull({st_code55.description}) OR TRIM({st_code55.description})= ""
)
then
"Unassigned"
else
(
if
(
{st_code55.description} startswith ["CTS", "PT", "OT"]
)
then
Nameparts [4] else
Nameparts[2])

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top