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 1092103
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T23:37:57+00:00 2026-05-16T23:37:57+00:00

I would like to have a TSQL Statement to move Name Suffix (Jr, Sr.,

  • 0

I would like to have a TSQL Statement to move Name Suffix (Jr, Sr., IV, etc) into another field.

The suffixes I see are
JR SR I II III IV V

Here is a sample of the data

LastName
BRUNNING, II
BURCH II
BUSS, JR.
CANI III
CHRISTIAN,SR
COLVIN Jr
COWHERD,JR.

I would like the suffix moved out of the LastName field into another field called Suffix.

LastName   Suffix  
BRUNNING   II
BURCH      I     
BUSS       JR
CANI       III
CHRISTIAN  SR
COLVIN     JR
COWHERD    JR

I am using SQL Server 2005 and can use SQL# functions.
Any help would be greatly appretiated.

  • 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. Editorial Team
    Editorial Team
    2026-05-16T23:37:57+00:00Added an answer on May 16, 2026 at 11:37 pm

    You can probably do better than this using the SQL# functions, but in straight T-SQL, here you go.

    The main idea here is to parse out the last segment/token in the name using REVERSE and PATINDEX, and then match it to a list of known suffixes.

    First some test data:

    IF OBJECT_ID('tempdb..#names') IS NOT NULL DROP TABLE #names
    IF OBJECT_ID('tempdb..#suffixes') IS NOT NULL DROP TABLE #suffixes
    CREATE TABLE #names (name VARCHAR(32))
    CREATE TABLE #suffixes (suffix VARCHAR(32))
    GO
    
    INSERT #names VALUES ('BRUNNING, II' )
    INSERT #names VALUES ('BURCH II'     )
    INSERT #names VALUES ('BUSS, JR.'    )
    INSERT #names VALUES ('CANI III'     )
    INSERT #names VALUES ('CHRISTIAN,SR' )
    INSERT #names VALUES ('COLVIN Jr'    )
    INSERT #names VALUES ('COWHERD,JR.'  )
    INSERT #names VALUES ('BILLY BOB'    )
    INSERT #names VALUES ('JOHNNY'       )
    
    INSERT #suffixes VALUES ('II' )
    INSERT #suffixes VALUES ('III')
    INSERT #suffixes VALUES ('JR' )
    INSERT #suffixes VALUES ('SR' )
    

    Then, an inline SELECT version. Notice the use of NULLIF to control for SUBSTRING errors.

    SELECT
      name
    , left_segments 
    , right_segment
    , new_name = CASE WHEN b.suffix IS NOT NULL THEN a.left_segments ELSE a.name END
    , b.suffix
    FROM (
      SELECT 
        name
      , left_segments = CASE WHEN left_segments LIKE '%[ ,]' THEN LEFT(left_segments,LEN(left_segments)-1) ELSE left_segments END
      , right_segment = CASE WHEN right_segment LIKE '%[.]' THEN LEFT(right_segment,LEN(right_segment)-1) ELSE right_segment END
      FROM (
        SELECT * 
        , left_segments = RTRIM(LEFT(RTRIM(name),LEN(name)-NULLIF(PATINDEX('%[ ,]%',REVERSE(RTRIM(name))),0)))
        , right_segment = RIGHT(RTRIM(name),NULLIF(PATINDEX('%[ ,]%',REVERSE(RTRIM(name))),0)-1)
        FROM #names
        ) a
      ) a
    LEFT JOIN #suffixes b ON a.right_segment = b.suffix
    

    Alternately, UPDATE w/ local vars:

    ALTER TABLE #names ADD 
      left_segments VARCHAR(64)
    , right_segment VARCHAR(64)
    GO
    
    DECLARE 
      @name VARCHAR(64)
    , @len INT
    , @last_delim INT
    , @left_segments VARCHAR(64)
    , @right_segment VARCHAR(64)
    
    UPDATE #names SET 
      @name           = RTRIM(name)
    , @len            = LEN(@name)
    , @last_delim     = @len-NULLIF(PATINDEX('%[ ,]%',REVERSE(@name)),0)
    , @left_segments  = RTRIM(LEFT(@name,@last_delim))
    , @right_segment  = RIGHT(@name,@len-@last_delim-1)
    , @left_segments  = CASE WHEN @left_segments LIKE '%[ ,]' THEN LEFT(@left_segments,LEN(@left_segments)-1) ELSE @left_segments END
    , @right_segment  = CASE WHEN @right_segment LIKE '%[.]'  THEN LEFT(@right_segment,LEN(@right_segment)-1) ELSE @right_segment END
    , left_segments   = @left_segments
    , right_segment   = @right_segment
    
    SELECT a.*
    , new_name = CASE WHEN b.suffix IS NOT NULL THEN a.left_segments ELSE a.name END
    , suffix = b.suffix
    FROM #names a LEFT JOIN #suffixes b ON a.right_segment = b.suffix
    

    The inline SELECT is fairly convenient, but difficult to read and troubleshoot. I prefer the UPDATE with local vars for anything I might have to return to later. Plus, it makes individual edits easier to apply.

    EDIT, SELECT method, slightly edited, and wrapped in an inline table-valued function. A inline TVF should be more efficient than a scalar UDF, and you get multiple return values to boot.

    CREATE FUNCTION dbo.ParseNameAndSuffix (@name VARCHAR(64), @ValidSuffixes VARCHAR(512))
    RETURNS TABLE AS RETURN (
      SELECT
        left_segments 
      , right_segment
      , new_name = CASE WHEN CHARINDEX(';'+right_segment+';',';'+@ValidSuffixes+';') > 0 THEN a.left_segments ELSE a.name END
      , suffix   = CASE WHEN CHARINDEX(';'+right_segment+';',';'+@ValidSuffixes+';') > 0 THEN a.right_segment END
      FROM (
        SELECT 
          name
        , left_segments = CASE WHEN left_segments LIKE '%[ ,]' THEN LEFT(left_segments,LEN(left_segments)-1) ELSE left_segments END
        , right_segment = CASE WHEN right_segment LIKE '%[.]' THEN LEFT(right_segment,LEN(right_segment)-1) ELSE right_segment END
        FROM (
          SELECT name
          , left_segments = RTRIM(LEFT(name,LEN(name)-NULLIF(PATINDEX('%[ ,]%',REVERSE(name)),0)))
          , right_segment = RIGHT(name,NULLIF(PATINDEX('%[ ,]%',REVERSE(name)),0)-1)
          FROM (SELECT name = LTRIM(RTRIM(@name))) a
          ) a
        ) a
      )
    GO
    
    SELECT * FROM #names a
    CROSS APPLY dbo.ParseNameAndSuffix(a.name,'II;III;JR;SR') b
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I would love to have a t-sql statement like the following... SELECT [field] FROM
I have a problem that I would like have solved via a SQL query.
So in my project i would like have a nice treeview that has images.
I would like to have a reference for the pros and cons of using
I would like to have an iframe take as much vertical space as it
I would like to have a VM to look at how applications appear and
We would like to have user defined formulas in our c++ program. e.g. The
I would like to have a nice template for doing this in development. How
I would like to have all developers on my team to use the same
I would like to have information about the icons which are displayed alongside the

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.