Parse a whole name field

roosterup
roosterup used Ask the Experts™
on
RPG code to extract first and last name from an "entire name" field.

I have a field that has clients' entire name.   Last name is always first, followed by a space.  I need to get all the characters until a space is found and put those chars into a last name field. Then, grab all the chars after the space and put those in the first name.


NAME
SMITH TED B.
SMITH RANDY
SMITH TOMMY R JR
JONES BRETT

to

LASTNAMEFLD                 FIRSTNAMEFLD
SMITH                                TED B.
SMITH                                RANDY
SMITH                                TOMMY R JR
JONES                                 BRETT
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
VP Technology / Senior Consultant
Commented:
One simple approach:

dcl-s firstSpacePos int(5);
dcl-s fullName  char(35) inz('PATTERSON GARY R.');
dcl-s lastName char(35);
dcl-s restOfName char(35);

// Find the first space in the name
firstSpacePos = %scan(' ' ; fullname);

// Extract the last name
lastName = %left(fullName : firstSpacePos-1);

// Extract rest of the name
restOfName = %right(fullName : %len(fullName) - firstSpacePos);

Author

Commented:
is %left valid?
Gary PattersonVP Technology / Senior Consultant

Commented:
Lol.  No SQL brain.  Just use %subst.

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial