• Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
    Dedicated community for Japanese speakers
  • 한국 커뮤니티
    Dedicated community for Korean speakers
Exit
0

How to extract last name from a string

Participant ,
Apr 03, 2014 Apr 03, 2014

Copy link to clipboard

Copied

I have SQL query that returns last name of a person and I need to extract the last part of any hyphenated or last names with a space in them

In cases where the person has one last name everything is OK

Like Smith. That OK. . I am looking for just Smith

The problem I have is when I have hyphenated last names like Scott-Thomas

In that case I want to extract the last last name or Thomas.

Or if I have a last name like Rodham Clinton.
In that case I only want to extract the last last name Clinton.

How do I do this?

Views

452

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines

correct answers 1 Correct answer

Guide , Apr 03, 2014 Apr 03, 2014

Creative use of the ListLast() function.  Something like:

<cfset variables.LastLastName = ListLast(myQuery.LastName, " -")>

Notice that the optional 2nd parameter, which is a non-default delimiter, has a space and a hyphen between the quotes.  This will cause ListLast() to break a string containing either a space or a hyphen into multiple parts, and return only the last part.

-Carl V.

Votes

Translate

Translate
Guide ,
Apr 03, 2014 Apr 03, 2014

Copy link to clipboard

Copied

Creative use of the ListLast() function.  Something like:

<cfset variables.LastLastName = ListLast(myQuery.LastName, " -")>

Notice that the optional 2nd parameter, which is a non-default delimiter, has a space and a hyphen between the quotes.  This will cause ListLast() to break a string containing either a space or a hyphen into multiple parts, and return only the last part.

-Carl V.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Apr 05, 2014 Apr 05, 2014

Copy link to clipboard

Copied

LATEST

<cfset variables.LastLastName = ListLast(myQuery.LastName, " -")>

That is the best solution, of course. However, the eye can easily miss the space.

The following slight change gives a bit of added value, for maintenance purposes:

<cfset variables.LastLastName = ListLast(myQuery.LastName, "#chr(32)#-")>

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Resources
Documentation