Display First Name using AMPScript code when it’s longer than three characters

Feb 14, 2023 | AMPScript, Marketing Cloud

f1-display-firstname-using-ampscript-code-kwamehq

To support my work, this post may contain affiliate links (these are referrer links sharing products and services. I get paid a small commission if you make a purchase through the link)

Display Firstname using Ampscript code.

In an ideal world, all customer data would be fully populated, accurate and the right length.

Sadly this is not the case, and you have to create a solution if you need to use certain variables as part of email personalisation.

In the example below, we’ll create an AMPScript code to display the first name within an email and create rules when the length is inadequate.

Where the first name is at least three characters, we’ll display the name within the email.

Where that is not the case, we’ll display the text “Hi“.

Display Firstname using Ampscript code

AMPSCript Code

The code below declares three variables that will be used in the solution

@min – stores the minimum character length set

@fname – stores the first name variable from your target data extension

@len – stores the character length of the first name variable

Where the length of the first name is equal to or greater than the min variable of 3, we output the first name variable.

In any other scenario, the text “Hi” is output.

%%[

var @min, @fname, @len

set @min = 3
set @fname = FIRST_NAME
set @len = length(@name)

if @len >= @min then
output(v(@fname))
else
output("Hi")
endif

]%%

Output Results

The table below shows the output of the results of first name values.

Number 1 and 2 displays the first name as they both are either equal to or greater than three characters.

Number 3, 4 and 5 are less than 3; thus, the default text of “Hi” is printed.

In the scenario in Number 5, the first name is blank/ null, so it returns a length of 0 which is less than the minimum set within the code.

No.FIRST_NAMEOUTPUT
1JamesJames
2JoeJoe
3MrHi
4PHi
5Hi

0 Comments

Submit a Comment

Pin It on Pinterest