0

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

4 Answers 4

2

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.

2
  • Thank you Naveen! It worked Jul 6, 2020 at 5:17
  • You are welcome @PrekshaRathore!. Can you please mark it as a right answer and close this thread.
    – Naveen VM
    Jul 6, 2020 at 7:31
1

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.

1

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)=%%
0

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

3
  • Wouldn't creating a rowset then parsing the rowset take more processing? You would need to create a temporary table, then parse out a row and then parse out a value from that row. Seems like heavier resource draw than indexing and splitting a string. Jul 6, 2020 at 23:46
  • @Gortonington I'm not actually sure about resources for one set of functions vs the other (I should redact that piece of my solution). That said, I have implemented the buildrowset solution in accounts that send to millions of subscribers in one send without any performance impact. It seems cleaner to me to treat the string like an array rather than searching for a string index and doing math. Jul 7, 2020 at 0:02
  • 1
    I certainly am not trying to discount your solution! I was mostly just curious about the performant part you had as I usually heard the opposite. But to be fair, the performance difference is likely to be small in either direction. Jul 7, 2020 at 1:27

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .