PROCEDURE parse_name (fullname_in IN VARCHAR2, fname_out IN OUT caller.first_name%TYPE, mname_out IN OUT caller.last_name%TYPE, lname_out IN OUT caller.middle_name%TYPE) /* || Note: comments are provided only for new elements of code. */ IS fullname_int VARCHAR2(255) := LTRIM (UPPER (fullname_in)); delim_comma NUMBER := INSTR (fullname_in, ','); /* Location of the first and last space delimiters. */ first_space NUMBER := INSTR (fullname_in, ' '); last_space NUMBER; BEGIN IF delim_comma = 0 AND first_space = 0 THEN /* Just one word, so it all goes to the last name */ lname_out := fullname_int; fname_out := NULL; mname_out := NULL; ELSIF delim_comma != 0 THEN /* LAST, FIRST or FIRST MIDDLE format: || Strip off last name, then do first and middle names */ lname_out := SUBSTR (fullname_int, 1, delim_comma-1); /* || Assign the remainder of the name to the first name. || If there are no more spaces, there is no middle name || and the first name will be the rest of the name. || || Find the first space after the comma. I use LTRIM || to remove any leading blanks between the comma and || the first name. */ fname_out := LTRIM (SUBSTR (fullname_int, delim_comma+1)); first_space := INSTR (fname_out, ' '); IF first_space = 0 THEN /* No space, so there is no middle name. */ mname_out := NULL; ELSE /* Split up the name into first and middle parts. */ fname_out := SUBSTR (fname_out, 1, first_space-1); mname_out := SUBSTR (fname_out, first_space+1); END IF; ELSIF first_space != 0 THEN /* || We have a "FIRST MIDDLE LAST" scenario. See if there is || another space, and make sure that there are non-blank || characters between the two blanks. If this is the case, || we have a middle name. If not, just split up the name into || first and last. Well? Didn't I say that names are || complicated? */ last_space := INSTR (fullname_in, ' ', -1, 1); IF first_space = last_space THEN /* Just one space. Split name as in parse_name. */ fname_out := SUBSTR (fullname_int, 1, first_space-1); lname_out := SUBSTR (fullname_int, 1, first_space+1); /* || Check to see if there is anything besides blanks between || the first and last names. The user might have typed: || "JOE SHMO" just to play games with your program. You || don't want to look silly, so you make the effort and || use RTRIM to squeeze out the blanks, leaving behind NULL || if that's all there was. */ ELSE /* || The hardest part about the middle name is that you have || to figure out its length. You need to subtract 1 from || the difference between last and first since the || first_space locator is actually always an offset from || zero. For example: || Loc = 12345678901234567890 || String = thelma hoke peterson || first_space (7) ||-^ ^||- last_space (12) || Length of middle name "Hoke" = 12 - 7 - 1 = 4 */ mname_out := LTRIM (RTRIM (SUBSTR (fullname_int, first_space + 1, last_space - first_space - 1))); /* Now the easy part. */ fname_out := SUBSTR (fullname_int, 1, first_space - 1); lname_out := SUBSTR (fullname_int, last_space + 1); END IF; END IF; END;