AMPscript Cheat Sheet
Short, correct AMPscript snippets for the things you write every week: variables, fallbacks, lookups, loops over rows, dates, strings and tracked links.

This is a working reference for AMPscript in Marketing Cloud Engagement emails, SMS and CloudPages. The snippets are short on purpose. They use sample data extension names such as Orders_DE and Loyalty_DE; swap in your own names and fields, and test every change with real and edge-case data before it goes near a live send.
Syntax basics
- Code blocks sit inside
%%[ ... ]%%. Inline output uses%%=v(@variable)=%%or%%=FunctionName(...)=%%. - Declare variables with
VAR, assign withSET. Variable names start with@. - String literals use double or single quotes. Function names and keywords are not case sensitive, but keep a consistent style.
- Comments use
/* ... */inside a code block. _subscriberkeyandemailaddrare system personalisation strings available in send context.
%%[
/* Greeting with a fallback */
VAR @firstName, @greeting
SET @firstName = AttributeValue("FirstName")
IF Empty(@firstName) THEN
SET @firstName = "there"
ENDIF
SET @greeting = Concat("Hi ", ProperCase(@firstName))
]%%
<p>%%=v(@greeting)=%%,</p>AttributeValue() returns an empty value rather than failing when the attribute is missing from the sending context, which makes it safer than referencing [FirstName] directly in shared content blocks.
Conditionals
Use IF ... ELSEIF ... ELSE ... ENDIF for anything with more than one outcome. For a simple inline choice, IIF(condition, valueIfTrue, valueIfFalse) keeps the HTML readable. Comparison operators are ==, !=, >, <, >=, <=, combined with AND, OR and NOT.
Lookups and loops
Lookup() returns one field from the first matching row. LookupRows() returns a rowset of matching rows, and LookupOrderedRows() lets you set how many rows to return and the sort order. Always check RowCount() before looping.
%%[
VAR @tier, @rows, @rowCount, @i, @row, @orderId, @total
SET @tier = Lookup("Loyalty_DE", "Tier", "SubscriberKey", _subscriberkey)
/* Three most recent orders, newest first */
SET @rows = LookupOrderedRows("Orders_DE", 3, "OrderDate DESC", "SubscriberKey", _subscriberkey)
SET @rowCount = RowCount(@rows)
IF @rowCount > 0 THEN
FOR @i = 1 TO @rowCount DO
SET @row = Row(@rows, @i)
SET @orderId = Field(@row, "OrderID")
SET @total = Field(@row, "OrderTotal")
]%%
<p>Order %%=v(@orderId)=%%: %%=FormatCurrency(@total, "en-GB")=%%</p>
%%[
NEXT @i
ELSE
]%%
<p>You have no recent orders.</p>
%%[
ENDIF
]%%Functions you will use most
| Function | What it does | Example |
|---|---|---|
| Empty(value) | True if the value is null or an empty string | IF Empty(@city) THEN |
| IIF(test, a, b) | Inline if | IIF(@tier == "Gold", "Gold member", "Member") |
| Concat(a, b, ...) | Joins strings | Concat(@first, " ", @last) |
| ProperCase, Uppercase, Lowercase | Change case | ProperCase(@firstName) |
| Trim(value) | Removes leading and trailing spaces | Trim(@postcode) |
| Substring(s, start, length) | Part of a string, starting at position 1 | Substring(@code, 1, 3) |
| IndexOf(s, search) | Position of a substring, counting from 1 | IndexOf(@email, "@") |
| Replace(s, find, with) | Replaces text | Replace(@phone, " ", "") |
| FormatNumber(n, format) | Formats numbers | FormatNumber(@points, "N0") |
Dates
Now() returns the current system date and time. Marketing Cloud system time is Central Standard Time without daylight saving adjustment, so convert with SystemDateToLocalDate() when you need local time. It uses the time zone set in the account or user settings, not the subscriber's own time zone, so check how it behaves in your account. Use DateAdd() and DateDiff() for arithmetic and FormatDate() for display.
%%[
VAR @expiry, @daysLeft
SET @expiry = DateAdd(Now(), 14, "D")
SET @daysLeft = DateDiff(Now(), @expiry, "D")
]%%
<p>Your offer ends on %%=FormatDate(@expiry, "dd MMMM yyyy")=%% (%%=v(@daysLeft)=%% days).</p>Links and CloudPages
- For a link built from variables, wrap the URL in
RedirectTo()inside thehrefso click tracking still works:<a href="%%=RedirectTo(@productUrl)=%%">View product</a>. CloudPagesURL(pageId, "key", value)builds an encrypted link to a CloudPage and passes subscriber context, which is safer than putting email addresses in query strings.- On a CloudPage,
RequestParameter("key")reads a posted or query string value. Validate and sanitise anything that arrives this way.
Stopping a send for one subscriber
RaiseError("Missing order data", true) skips the current subscriber instead of sending a broken email. Use it when required data is missing, and monitor how often it fires: a sudden increase usually means an upstream data feed has failed.
Common mistakes
- Looping without checking
RowCount()first, which breaks when there are no rows. - Assuming
Lookup()returns the newest row. It returns the first match; useLookupOrderedRows()when order matters. - Comparing numbers stored as text. Check the data extension field type before using
>or<. - Personalising subject lines with fields that are often empty, with no fallback.
- Testing only with a perfect test record. Always preview with a subscriber whose data is incomplete.
For deeper guidance see LookupRows vs LookupOrderedRows, how to debug AMPscript personalisation errors, AMPscript vs Server-Side JavaScript and our AMPscript development service. The full function reference lives in the Salesforce developer documentation.
Need AMPscript you can trust?
We write, review and debug AMPscript for emails, SMS and CloudPages, with test cases for the edge cases.




