Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 54663
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 10, 20262026-05-10T17:15:19+00:00 2026-05-10T17:15:19+00:00

How do I parse the first, middle, and last name out of a fullname

  • 0

How do I parse the first, middle, and last name out of a fullname field with SQL?

I need to try to match up on names that are not a direct match on full name. I’d like to be able to take the full name field and break it up into first, middle and last name.

The data does not include any prefixes or suffixes. The middle name is optional. The data is formatted ‘First Middle Last’.

I’m interested in some practical solutions to get me 90% of the way there. As it has been stated, this is a complex problem, so I’ll handle special cases individually.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. 2026-05-10T17:15:19+00:00Added an answer on May 10, 2026 at 5:15 pm

    Here is a self-contained example, with easily manipulated test data.

    With this example, if you have a name with more than three parts, then all the ‘extra’ stuff will get put in the LAST_NAME field. An exception is made for specific strings that are identified as ‘titles’, such as ‘DR’, ‘MRS’, and ‘MR’.

    If the middle name is missing, then you just get FIRST_NAME and LAST_NAME (MIDDLE_NAME will be NULL).

    You could smash it into a giant nested blob of SUBSTRINGs, but readability is hard enough as it is when you do this in SQL.

    Edit– Handle the following special cases:

    1 – The NAME field is NULL

    2 – The NAME field contains leading / trailing spaces

    3 – The NAME field has > 1 consecutive space within the name

    4 – The NAME field contains ONLY the first name

    5 – Include the original full name in the final output as a separate column, for readability

    6 – Handle a specific list of prefixes as a separate ‘title’ column

    SELECT   FIRST_NAME.ORIGINAL_INPUT_DATA  ,FIRST_NAME.TITLE  ,FIRST_NAME.FIRST_NAME  ,CASE WHEN 0 = CHARINDEX(' ',FIRST_NAME.REST_OF_NAME)        THEN NULL  --no more spaces?  assume rest is the last name        ELSE SUBSTRING(                        FIRST_NAME.REST_OF_NAME                       ,1                       ,CHARINDEX(' ',FIRST_NAME.REST_OF_NAME)-1                      )        END AS MIDDLE_NAME  ,SUBSTRING(              FIRST_NAME.REST_OF_NAME             ,1 + CHARINDEX(' ',FIRST_NAME.REST_OF_NAME)             ,LEN(FIRST_NAME.REST_OF_NAME)            ) AS LAST_NAME FROM   (     SELECT     TITLE.TITLE    ,CASE WHEN 0 = CHARINDEX(' ',TITLE.REST_OF_NAME)          THEN TITLE.REST_OF_NAME --No space? return the whole thing          ELSE SUBSTRING(                          TITLE.REST_OF_NAME                         ,1                         ,CHARINDEX(' ',TITLE.REST_OF_NAME)-1                        )     END AS FIRST_NAME    ,CASE WHEN 0 = CHARINDEX(' ',TITLE.REST_OF_NAME)            THEN NULL  --no spaces @ all?  then 1st name is all we have          ELSE SUBSTRING(                          TITLE.REST_OF_NAME                         ,CHARINDEX(' ',TITLE.REST_OF_NAME)+1                         ,LEN(TITLE.REST_OF_NAME)                        )     END AS REST_OF_NAME    ,TITLE.ORIGINAL_INPUT_DATA   FROM     (        SELECT       --if the first three characters are in this list,       --then pull it as a 'title'.  otherwise return NULL for title.       CASE WHEN SUBSTRING(TEST_DATA.FULL_NAME,1,3) IN ('MR ','MS ','DR ','MRS')            THEN LTRIM(RTRIM(SUBSTRING(TEST_DATA.FULL_NAME,1,3)))            ELSE NULL            END AS TITLE       --if you change the list, don't forget to change it here, too.       --so much for the DRY prinicple...      ,CASE WHEN SUBSTRING(TEST_DATA.FULL_NAME,1,3) IN ('MR ','MS ','DR ','MRS')            THEN LTRIM(RTRIM(SUBSTRING(TEST_DATA.FULL_NAME,4,LEN(TEST_DATA.FULL_NAME))))            ELSE LTRIM(RTRIM(TEST_DATA.FULL_NAME))            END AS REST_OF_NAME      ,TEST_DATA.ORIGINAL_INPUT_DATA     FROM       (       SELECT         --trim leading & trailing spaces before trying to process         --disallow extra spaces *within* the name         REPLACE(REPLACE(LTRIM(RTRIM(FULL_NAME)),'  ',' '),'  ',' ') AS FULL_NAME        ,FULL_NAME AS ORIGINAL_INPUT_DATA       FROM         (         --if you use this, then replace the following         --block with your actual table               SELECT 'GEORGE W BUSH' AS FULL_NAME         UNION SELECT 'SUSAN B ANTHONY' AS FULL_NAME         UNION SELECT 'ALEXANDER HAMILTON' AS FULL_NAME         UNION SELECT 'OSAMA BIN LADEN JR' AS FULL_NAME         UNION SELECT 'MARTIN J VAN BUREN SENIOR III' AS FULL_NAME         UNION SELECT 'TOMMY' AS FULL_NAME         UNION SELECT 'BILLY' AS FULL_NAME         UNION SELECT NULL AS FULL_NAME         UNION SELECT ' ' AS FULL_NAME         UNION SELECT '    JOHN  JACOB     SMITH' AS FULL_NAME         UNION SELECT ' DR  SANJAY       GUPTA' AS FULL_NAME         UNION SELECT 'DR JOHN S HOPKINS' AS FULL_NAME         UNION SELECT ' MRS  SUSAN ADAMS' AS FULL_NAME         UNION SELECT ' MS AUGUSTA  ADA   KING ' AS FULL_NAME               ) RAW_DATA       ) TEST_DATA     ) TITLE   ) FIRST_NAME 
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 76k
  • Answers 76k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • added an answer Did you read back the value from the cookie at… May 11, 2026 at 3:04 pm
  • added an answer I would not dirty your control version system with such… May 11, 2026 at 3:04 pm
  • added an answer Many of the social networking sites like Twitter don't use… May 11, 2026 at 3:04 pm

Related Questions

I have a dataset obtained from MySQL that goes like this: Array ( [0]
Given this text: /* F004 (0309)00 */ /* field 1 */ /* field 2
Object o = new Long[0] System.out.println( o.getClass().isArray() ) System.out.println( o.getClass().getName() ) Class ofArray =
I have a Rails site, where the content is written in markdown. I wish

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.