Parse a string to pull second of three words

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • JM420A
    New Member
    • Jul 2008
    • 8

    Parse a string to pull second of three words

    Access 2k3

    Query:

    I have a string: DOE JOHN MICHAEL (last first middle)
    I need to pull JOHN out of it

    I can pull out DOE, and get the length of DOE, what I'm having trouble with is how to pull JOHN out. I've been working on it for a while and getting frustrated, so I'm coming to the pros for help.


    Here is what I have so far.
    Code:
    SELECT Left(tblUnit_Data_Current.NAME,InStr(1, tblUnit_Data_Current.NAME," ")-1) as LastName,
    
    Len (left(tblUnit_Data_Current.NAME,InStr(1, tblUnit_Data_Current.NAME," ")-1)) as LastNameLength
    
    FROM tblUnit_Data_Current;
    Any help, guidance or direction is appreciated.

    thx

    jm420a
    Last edited by NeoPa; Aug 5 '08, 08:01 PM. Reason: Please use the [CODE] tags provided
  • missinglinq
    Recognized Expert Specialist
    • Nov 2006
    • 3533

    #2
    For simplicity's sake, I'll call the string NameString:
    Code:
    Mid(NameString, InStr(NameString, " ") + 1, InStrRev(NameString, " ") - InStr(NameString, " ") - 1)
    This has been tested with a variety of first, middle and last names. It does, however, require that the string has at least a middle initial, if not a full middle name.

    Welcome to Bytes!

    Linq ;0)>

    Comment

    • NeoPa
      Recognized Expert Moderator MVP
      • Oct 2006
      • 32634

      #3
      There are other ways (specifically using Split()) if VBA is used. How it is best done does depend on exactly what can be expected from the data and what is expected in the output.

      Are the separator characters always spaces for instance?

      Is calling a VBA procedure an acceptable solution?

      Comment

      Working...