AMPscript Substring (Next)
Take part of a string by position with AMPscript Substring in Marketing Cloud Next. Syntax, examples, and behaviour proven in a Marketing Cloud Next email, including how it differs from Engagement.
Syntax
Substring(sourceString, startPosition[, substringLength]) → string
Parameters
| Name | Type | Required | Description |
|---|---|---|---|
sourceString |
string | Yes | String to take a portion of |
startPosition |
number | Yes | 1-based position to start at, inside the source |
substringLength |
number | No | Number of characters to take |
Pass the source as a string and both positions as unquoted numbers. A numeric source, a quoted number or a start outside the source breaks the render.
Example
%%=Substring("Hello World", 1, 5)=%%
Renders Hello.
Leave out the length to take everything from the start position on:
%%=Substring("Hello World", 7)=%%
Renders World.
Return value
string — the requested portion of the source.
An empty string comes back for a length of 0 and for an empty source. Otherwise the result is free text, so there is no fixed set of special values to test for.
Behaviour
Positions are 1-based. Substring("Hello World", 1, 5) gives Hello, Substring("Hello World", 7, 3) gives Wor, and Substring("Hello World", 11, 1) gives the final d.
Without a length, the rest of the string is returned. Substring("Hello World", 7) gives World and Substring("Hello World", 11) gives d.
A length past the end is capped. Substring("Hello World", 7, 99) gives World.
A length of 0 or an empty source give an empty string. Substring("Hello World", 3, 0) and Substring("", 1, 3) both render nothing.
The start must lie inside the source. The email saves, but the preview fails (HTTP 500, or no new rendered version) for a start of 0, a negative start such as -3, and a start past the end: Substring("Hello World", 20, 3) and Substring("Hello World", 12) both failed. Check the position against Length before the call.
A negative length breaks the render too. Substring("Hello World", 3, -2) failed in the preview.
Decimals are cut down to whole numbers. Substring("Hello World", 2.5, 3) gives ell and Substring("Hello World", 1, 3.7) gives Hel: the fraction is dropped rather than rejected.
Only strings as the source, only numbers as positions. The preview failed for a numeric source (Substring(9876543, 3, 2)), a boolean source (Substring(true, 1, 2)), quoted positions (Substring("Hello World", "7", 3), Substring("Hello World", 7, "3"), Substring("Hello World", "1", "5")), a boolean position and a lettered position such as "abc".
Counting matches Length. In a source built as Concat("caf", Char(233), " time"), one character taken at position 4 has a Length of 1, and starting at 6 gives time.
Two or three arguments. Substring(), Substring("Hello World") and a four-argument call cannot be saved: the content API answers HTTP 400 with “The AMPscript syntax isn’t valid”.
Show Next test script
<!-- Marketing Cloud Next test script for Substring. Paste it into the HTML component of a
Next email and open the preview. Each line prints the result Next gave when the call
was rendered on its own (expect) next to the result of this run (got).
Assembled from single-call test emails (dates on the page); it has not been
run as one email. Calls that make Next fail are listed separately. -->
<p>c1 expect=[Hello] got=[%%=Substring("Hello World",1,5)=%%]</p>
<p>c2 expect=[World] got=[%%=Substring("Hello World",7)=%%]</p>
<p>c3 expect=[World] got=[%%=Substring("Hello World",7,99)=%%]</p>
<p>c4 expect=[Wor] got=[%%=Substring("Hello World", 7, 3)=%%]</p>
<p>c5 expect=[d] got=[%%=Substring("Hello World", 11, 1)=%%]</p>
<p>c6 expect=[d] got=[%%=Substring("Hello World", 11)=%%]</p>
<p>c7 expect=[] got=[%%=Substring("", 1, 3)=%%]</p>
<p>c8 expect=[] got=[%%=Substring("Hello World", 3, 0)=%%]</p>
<p>c9 expect=[1] got=[%%=Length(Substring(Concat("caf",Char(233)," time"), 4, 1))=%%]</p>
<p>c10 expect=[time] got=[%%=Substring(Concat("caf",Char(233)," time"), 6)=%%]</p>
<p>c11 expect=[ell] got=[%%=Substring("Hello World", 2.5, 3)=%%]</p>
<p>c12 expect=[Hel] got=[%%=Substring("Hello World", 1, 3.7)=%%]</p>
Show calls that fail in Next (one email each)
<!-- Run each call below in its own Next email: one of them in a combined email
would stop the whole preview or block the save. -->
<!-- email 1: expect that the email saves, but the preview fails (HTTP 500) -->
%%=Substring("Hello World",0,5)=%%
<!-- email 2: expect that the email saves, but the preview never shows the new version (no fresh render) -->
%%=Substring("Hello World", 20, 3)=%%
<!-- email 3: expect that the email saves, but the preview fails (HTTP 500) -->
%%=Substring("Hello World", 12)=%%
<!-- email 4: expect that the email saves, but the preview fails (HTTP 500) -->
%%=Substring("Hello World", -3, 5)=%%
<!-- email 5: expect that the email saves, but the preview fails (HTTP 500) -->
%%=Substring("Hello World", "7", 3)=%%
<!-- email 6: expect that the email saves, but the preview never shows the new version (no fresh render) -->
%%=Substring("Hello World", 7, "3")=%%
<!-- email 7: expect that the email saves, but the preview fails (HTTP 500) -->
%%=Substring("Hello World", "1", "5")=%%
<!-- email 8: expect that the email saves, but the preview fails (HTTP 500) -->
%%=Substring(9876543, 3, 2)=%%
<!-- email 9: expect that the email saves, but the preview fails (HTTP 500) -->
%%=Substring(true, 1, 2)=%%
<!-- email 10: expect that the email saves, but the preview fails (HTTP 500) -->
%%=Substring("Hello World", 3, -2)=%%
<!-- email 11: expect that the email saves, but the preview fails (HTTP 500) -->
%%=Substring("Hello World", true, 5)=%%
<!-- email 12: expect that the email saves, but the preview fails (HTTP 500) -->
%%=Substring("Hello World", 1, true)=%%
<!-- email 13: expect that the email saves, but the preview fails (HTTP 500) -->
%%=Substring("Hello World", "abc", 3)=%%
<!-- email 14: expect that the save is rejected (HTTP 400) -->
%%=Substring()=%%
<!-- email 15: expect that the save is rejected (HTTP 400) -->
%%=Substring("Hello World")=%%
<!-- email 16: expect that the save is rejected (HTTP 400) -->
%%=Substring("Hello World", 1, 5, 2)=%%
Differs from official docs
- A start past the end does not give an empty string. The reference promises an empty string there; in Next
Substring("Hello World", 12)andSubstring("Hello World", 20, 3)made the preview fail, as did a start of0or below. Card - Positions as text and numeric sources break the render. The reference does not cover them; in Next
Substring("Hello World", "7", 3)andSubstring(9876543, 3, 2)made the preview fail. Card
Availability & differences
| Platform | Available |
|---|---|
| Marketing Cloud Engagement | Yes |
| Marketing Cloud Next | Yes, from API 67.0 |
Engagement clamps a start below 1 (Substring("Hello World", 0, 5) gives Hello), returns an empty string for a start past the end, and accepts numeric sources and quoted numbers; in Next all of these make the email preview fail. Engagement aborts on a decimal position or length, while Next drops the fraction (Substring("Hello World", 2.5, 3) gives ell). Compared 2026-11-23 by rendering the same calls in a Marketing Cloud Next email.
See also
IndexOf— finds the position to start atLength— measures the source before you cut it- Differs from official docs (Next)
- Official reference