19
Views
5
Comments
Solved
How to get only the last name?
Application Type
Traditional Web

I keep the first and last name of the same place in the database, just as a name. 

How do I get the last name? 

Example in the database I have customer name = John Doe and I only want Doe.

Thanks

Rank: #86
Solution

Hello Rita,

Hope you're doing well.

There are several ways to do that. And that depends if you only have the first and last name or if you can have more than just those 2 names :)


If you want a generic approach, I'd suggest that you use String_Split action from Text extension defining a space ( " " ) as a Delimiter and passing your name as Text.

This will return a list with the several strings separated by the delimiter. The last name will be the last record of this list.

For you example ("John Doe"), this action returns a list with 2 elements:
- Element 1, Position 0 = "John"
- Element 2, Position 1 = "Doe"


Another approach will be the usage of Substr() and Index() built-in functions. If you only have the first and last name, you can follow this simpler approach: use Index() built-in function to search for the space ( " " ) and then Substr() to cut the part that you pretend.

Something like this should do the trick:

Substr(FullName, Index(FullName, " ") + 1, Length(FullName))


Hope that this helps you!


Kind regards,

Rui Barradas

Rank: #639
Solution

Hi Rita,

On top of what was already exposed, there is also the possibility to use Regex.

You can use Regex_Search Action that is located under Text Extension. You can give it a Pattern like " \S+$ " and it will return you the last word. This pattern will also grab names that contain symbols like "-" and ".".

Another thing that you should think about is when you find persons with names like "Pedro Alves Carmo De Sousa". Normally when giving you their First and Last Name they will say "Pedro De Sousa". With the approach that we are following, you will grab only "Sousa".

I guess Craig's suggestion of adding attributes is something for you to think about.

However, with the above options offered by members of this community, you can tackle your issue. 

Best Regards,

João Delgado

Rank: #86
Solution

Hello Rita,

Hope you're doing well.

There are several ways to do that. And that depends if you only have the first and last name or if you can have more than just those 2 names :)


If you want a generic approach, I'd suggest that you use String_Split action from Text extension defining a space ( " " ) as a Delimiter and passing your name as Text.

This will return a list with the several strings separated by the delimiter. The last name will be the last record of this list.

For you example ("John Doe"), this action returns a list with 2 elements:
- Element 1, Position 0 = "John"
- Element 2, Position 1 = "Doe"


Another approach will be the usage of Substr() and Index() built-in functions. If you only have the first and last name, you can follow this simpler approach: use Index() built-in function to search for the space ( " " ) and then Substr() to cut the part that you pretend.

Something like this should do the trick:

Substr(FullName, Index(FullName, " ") + 1, Length(FullName))


Hope that this helps you!


Kind regards,

Rui Barradas

Champion
Rank: #39

Hi,


As Rui said String_Split is the most appropriate action,you can do to achieve your expectation.

mvp_badge
MVP
Rank: #177

Hello Rita,

To add to what Rui and Salman said, you can certainly get the last portion of the name with String_Split, however that will not work for all last names (for example any name that starts with St.). It will also be problematic for some people with multiple names in their surname.

My recommendation would be to use your "Name" attribute as a "Display Name", and then add attributes for given name and surname (or First Name and Last Name). You can autopopulate those from your String_Split, but then allow the user to modify them in their user profile.

Rank: #639
Solution

Hi Rita,

On top of what was already exposed, there is also the possibility to use Regex.

You can use Regex_Search Action that is located under Text Extension. You can give it a Pattern like " \S+$ " and it will return you the last word. This pattern will also grab names that contain symbols like "-" and ".".

Another thing that you should think about is when you find persons with names like "Pedro Alves Carmo De Sousa". Normally when giving you their First and Last Name they will say "Pedro De Sousa". With the approach that we are following, you will grab only "Sousa".

I guess Craig's suggestion of adding attributes is something for you to think about.

However, with the above options offered by members of this community, you can tackle your issue. 

Best Regards,

João Delgado

Rank: #14732

Hi,

Thank you all for your time.