0

With AMPscript is there any way to check a field to see if it has one letter in it? For example, in the first name field examples below, could I detect that there is a single J in that field? I don't need to know that it is a J, but just a single letter?

John J

J John

6
  • so you want to be able to detect if there are any 'words' in the string that are just a single character long? This can be done somewhat in AMPscript, but likely would be tremendously easier and more performant to be done via SQL if possible. What is your purpose here? I think this may be a bit of an XY problem that there is another option beyond this ask that can better solve your problem. Commented May 2, 2022 at 17:07
  • Yes, I want to determine if there is just a single character in a name field (John J) for example. The purpose is that they want to control how the salutation looks in the emails sent. So there are some people that have John J or J John in their name, when that happens they want to change the salutation .
    – gh0st
    Commented May 2, 2022 at 17:12
  • Is this on a more schedule send that you could use SQL to put a flag in your sendable DE to use to alter the salutation or is this more 'real time' that would require it to be in the email itself? Commented May 2, 2022 at 17:21
  • It is more of a real time situation, because it's not always the same DE.
    – gh0st
    Commented May 2, 2022 at 17:25
  • I would be interested in the SQL to check for one character in a field that could look like (John H or K Smith)? Thanks
    – gh0st
    Commented May 2, 2022 at 17:38

1 Answer 1

0

Based off of the info from the comments mentioning this has to be done in email in 'real time', the easiest way I can think of to test this via AMPscript is to do the following:

  1. Set the string into an AMPscript variable
  2. Build a rowset from that string using space as the delimiter
  3. Iterate through the rowset and count the length of each row
  4. If you find a row with a count of 1 - set an ampscript var as a flag
  5. Use that flag for deciding which salutation to use.

For example:

%%[
  SET @flag = 0
  SET @fName = AttributeValue("FirstName")
  SET @fName_RS = BuildRowsetFromString(@fName," ")
  
  FOR @i=1 TO RowCount(@fName_RS) DO
    SET @fName_R = Row(@fName_RS,@i)
    SET @temp = Field(@fName_R,1)
    SET @length = Length(@temp)
    
    IF @length == 1 THEN
      SET @flag = 1
    ENDIF
  NEXT @i

  IF @flag == 1 THEN
    SET @salutation = "One letter variant"
  ELSE
    SET @salutation = "Other version"
  ENDIF
]%%
2
  • 1
    You, my friend, are a genius. Thank you!
    – gh0st
    Commented May 2, 2022 at 18:19
  • @gh0st - You might want to put an IF around the for loop so it only runs if RowCount(@fName_RS) > 0 to help remove potential error if that field is empty and Rowcount is 0. Commented May 2, 2022 at 19:34

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .