3

I'm guessing there is a more efficient way for me to handle the following scenario and am looking for some advice.

Say I have 20 different Cities, abbreviated in my database by 3 letters. LOS, for example is Los Angeles. I have to dynamically populate the city name in an email based on these three letters. Assuming I can't change the data itself, is there a better way to handle it than a series of if/thens?

%%[if @cityCode == "LOS"  then]%%
Los Angeles
%%[ ELSEIF @CityCode == "SEA"  then]%%
Seattle
%%[endif]%%
3

You can load the city codes in a Data Extension and do a lookup.

%%[

var @cityCode, @city
set @cityCode = AttributeValue("cityCode")
set @city = Lookup("cities", "city", "cityCode", @cityCode)

]%%
<br>cityCode: %%=v(@cityCode)=%%
<br>City: %%=v(@city)=%%
0

In my experience, using a lookup table is a cleaner, less confusing way to avoid dozens of IF statements. However, if you can't or don't want to; try IndexOf(). The code below has been tried and tested.

I don't have any city codes, but let's pretend these are some. The spaces in between the codes serve as delimiters (you could use almost any symbol, as long as it's not in ANY of the codes). IndexOf() brings back the number at which the code appears. If a number greater than zero is returned, the code was found. If a zero is returned, then the code was not found.

SET @MyCityCode = "FD0"

SET @ListOfCodes =  " AF2 AG2 AH2 AI2 AJ2 AK2 BF2 BG2 BH2 BI2 BJ2 BK2 CF2 CG2 CH2 CI2 CJ2 CK2 FA0 FB0 FC0 FD0 FE0 FF0 FG0 FH0 FI0 FJ0 FK0 "

IF  IndexOf(@ListOfCodes, @MyCityCode) > 0  THEN
   /* My city is in the list */
ELSE
   /* My city is NOT in the list */
ENDIF

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.