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

  • SEARCH
  • Home
  • 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 7168013
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T14:41:37+00:00 2026-05-28T14:41:37+00:00

I have a column name that represents a person’s name in the following format:

  • 0

I have a column name that represents a person’s name in the following format:

firstname [middlename] lastname [, Sr.|Jr.]

For, example:

John Smith
John J. Smith
John J. Smith, Sr.

How can I order items by lastname?

  • 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-28T14:41:38+00:00Added an answer on May 28, 2026 at 2:41 pm

    A correct and faster version could look like this:

    SELECT *
    FROM   tbl
    ORDER  BY substring(name, '([^[:space:]]+)(?:,|$)')
    

    Or:

    ORDER  BY substring(name, E'([^\\s]+)(?:,|$)')
    

    Or even:

    ORDER  BY substring(name, E'([^\\s]+)(,|$)')
    

    Explain

    [^[:space:]]+ .. first (and longest) string consisting of one or more non-whitespace characters.
    (,|$) .. terminated by a comma or the end of the string.

    The last two examples use escape-string syntax and the class-shorthand \s instead of the long form [[:space:]] (which loses the outer level of brackets when inside a character class).

    We don’t actually have to use non-capturing parenthesis (?:) after the part we want to extract, because (quoting the manual):

    .. if the pattern contains any parentheses, the portion of the text that
    matched the first parenthesized subexpression (the one whose left
    parenthesis comes first) is returned.

    Test

    SELECT substring(name, '([^[:space:]]+)(?:,|$)')
    FROM  (VALUES 
      ('John Smith')
     ,('John J. Smith')
     ,('John J. Smith, Sr.')
     ,('foo bar Smith, Jr.')
    ) x(name)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a column Name Quote Price in a DataGridView winforms control. I can
I have datatable with column name tag and 100 rows of data.I need to
In mysql database i have this column called: Name: Date Type: datetime I have
I have two or more tables which hold values under the same column name.
I have a DataGrid table with columns name , phone . In column name
I have a LINQ to SQL generated class with a readonly property: <Column(Name:=totalLogins, Storage:=_TotalLogins,
I have a VARCHAR(4) column that receives data from an input that may be
I have a ViewModel that represents multiple options and implements IDataErrorInfo . This ViewModel
I have an SSIS package that does the following: Selects the connection strings from
So I have a Status table (with an id and a name column), it

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.