Ampscript to render First name from Full name not showing when there's only one Name (one word)

I have a DE which has Full names in it. I wanted to extract the first name from it, so I wrote the following ampscript:

%%[ 

var @fullName 
var @firstName 
set @fullName = AttributeValue("fullName") 

if indexOf(@fullName, " ") > 0 then 
    set @firstName = Substring(@fullName,1, Subtract(IndexOf(@fullName," "),1)) 
endif 

]%% 
%%=v(@firstName)=%%

This worked! If the Full Name was "Mouse Keyboard", then it would only display "Mouse".
However, I realised some people have only written their ‘First Name’ in ‘Full Name’.. So with the above ampscript, if someone has written simply ‘Mouse’ under Full Name, it won’t display anything, it’ll be left blank.

I can’t figure how to edit the above ampscript to do two things at once:

  • Separate first name from ‘Full Name’ [which I’ve done]
  • But, if there’s only the first name (one word) under ‘Full Name’, then simply display that

Here is Solutions:

We have many solutions to this problem, But we recommend you to use the first solution because it is tested & true solution that will 100% work for you.

Solution 1

You are on right track, when the First Name is the only word, just add it in the else block. Below is your updated code.

%%[ 
var @fullName,@firstName 
set @fullName = AttributeValue("fullName") 
if indexOf(@fullName, " ") > 0 then 
set @firstName = Substring(@fullName,1, Subtract(IndexOf(@fullName," "),1)) 
Else
set @firstName = AttributeValue("fullName") 
endif ]%% 
%%=v(@firstName)=%%

Let me know if this helps.

Solution 2

To keep things a bit cleaner, I do similar to @NaveenVM but with a little bit of a twist. I wanted to share as an alternative in case anyone prefers it this way.

%%[
SET @firstName = AttributeValue("fullName")
SET @index = IndexOf(@firstName," ")

IIF(@index > 0,SET @firstName = Substring(@firstName,1,Subtract(@index,1)),'')
]%%

This will by default have the @firstName variable set to the full name, so if there is only the single name, it will default to existing name – removing the need for an else statement.

I also created an @index variable to hold the value of the index for easier reference and changed the if statement into an inline if statement to reduce the characters needed.

Solution 3

You could also utilize the trim function:

%%[

set @name = AttributeValue("fullName")
set @index = IndexOf(@firstName," ")

if @index > 0 then
    set @name = trim(Substring(@name,1,@index))
endif

]%%
%%=v(@name)=%%

Solution 4

If I’m processing words in a string, I prefer BuildRowSetFromString.

First example is breaking out everything for commenting:

SET @fullName = AttributeValue("fullName")
SET @nameRows = BuildRowSetFromString(@fullName, ' ') /* split on space */
SET @nameRow = Row(@nameRows, 1) /* first row, which should contain first name */
SET @firstName = Field(@nameRow, 1) /* 1 indicates you want the first value from the row since none of the data is named */

Now, if you wanted to do that in one line:

SET @firstName = Field(Row(BuildRowSetFromString(AttributeValue("fullName"), ' '), 1), 1)

The nice thing about BuildRowSetFromString is that if there are no spaces in your fullName field, it will give you the entire fullName value as the first record of the first row of the rowset it returns. It’s also much cleaner than indexOf + substring

Note: Use and implement solution 1 because this method fully tested our system.
Thank you 🙂

All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

Leave a Reply