Substring Function

The Substring function is similar to the =LEFT or =RIGHT function in Excel. Using the Substring function lets your return a portion of a string based on the position where you want to start and the length of the string. A good use case is using Substring to return the last 4 digits of a phone number.



1 = Initial string value (can be a variable)
2 = Character position to begin. Starting from the left where 0 is the first position
3 = Max number of character length in output




%%[ var @phone, @last4phone
Set @phone = [Phone]
set @last4phone = Substring(@phone, 9,4) ]%%
Last 4 digits of your phone: %%=v(@last4phone)=%%


Data Extension:



Last 4 digits of your phone: 1234


In the AMPscript above, first we are declaring the variables using the var function. Next, refer to the sample data extension above — the AMPscript is setting the variable of @phone to the Phone column in the data extension.

Then setting a new variable of @last4phone, we use the Substring function. The variable of @phone is used as the initial value. The character position to start is 9 characters including counting the dashes (-). Counting from the value of 310-123-1234, the 9th character is the second number of 1. The maximum character output is 4. The AMPscript Substring function returns the value of “1234”

Leave a Reply

Your email address will not be published. Required fields are marked *