Substring

This function returns a portion of the specified string starting at a certain character position and no longer than the specified length. If the specified character position is more than the length of the specified string, the function returns an empty string. If you do not specify a value for the third parameter, the function will return the remainder of the string.

Arguments

Substring(1,2,3)

Ordinal Type Required Description
1 String True The string from which to extract a portion
2 Number True Starting position of substring
3 Number False Length of substring. If not specified, value defaults to the remaining length of the source string.

Example 1

%%[

var @fullName
var @firstName

set @fullName = AttributeValue("fullName") /* value from attribute or DE column in send context */
set @fullName = "Curt Harris" /* or a literal value */

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

]%%
fullName: %%=v(@fullName)=%%
<br>firstName: %%=v(@firstName)=%%

Output

fullName: Curt Harris
firstName: Curt

Example 2

%%[

var @fullName
var @lastName

set @fullName = AttributeValue("fullName") /* value from attribute or DE column in send context */
set @fullName = "Barb Brown" /* or a literal value */

if indexOf(@fullName, " ") > 0 then
  set @lastName = Substring(@fullName,Add(indexOf(@fullName, " "),1))
endif

]%%
fullName: %%=v(@fullName)=%%
<br>lastName: %%=v(@lastName)=%%

Output

fullName: Barb Brown
lastName: Brown