Using beast mode to compare strings

Options

Hi,

I am trying to compare the values of 2 fields using a CASE WHEN statement.

"field1" contains an email address e.g. "bob@test.com"

"field2" may contain a name and email, e.g. "Bob Smith, bob@test.com"

The outcome I would like from my beast mode is to check if the value in field1 is contained within the value of field2. I cannot guarantee the layout of field2 as it has been manually input so the email may be missing or not be in the last part of the string (just to rule out anything that might work by evaluating the last x characters of the string, it needs to look through the whole string somehow)

What I have been toying with is like this:

CASE WHEN 'field1' LIKE '%'field2'%' THEN 'Looks good'

ELSE 'Looks bad'

END

Any suggestions please?

Best Answers

  • MichelleH
    MichelleH Coach
    Answer ✓
    Options

    @Mark Puddephatt You can use the INSTR function to return the starting position of the email address within field2. If field1 is contained within field2 it will return a number greater than 0.

    case when INSTR(field2,field1) > 0 then 'Looks good' else 'Looks bad' end
    

  • Sean_Tully
    Sean_Tully Contributor
    Answer ✓
    Options

    The upper function just helps standardize the strings so that they'll match even if they have different cases within the data.

    For "matches field 3" you would just add another line to identify them:

    case when instr(upper(`field2`), upper(`field1`)) > 0 then 'matches field 2'

    when instr(upper(`field3`), upper(`field1`)) > 0 then 'matches field 3'

    else 'Looks bad'

Answers

  • Sean_Tully
    Sean_Tully Contributor
    Options

    I'd probably do something like

    case when instr(upper(`field2`), upper(`field1`)) > 0 then 'Looks good'

    else 'Looks bad'

    end

  • MichelleH
    MichelleH Coach
    Answer ✓
    Options

    @Mark Puddephatt You can use the INSTR function to return the starting position of the email address within field2. If field1 is contained within field2 it will return a number greater than 0.

    case when INSTR(field2,field1) > 0 then 'Looks good' else 'Looks bad' end
    

  • Mark Puddephatt
    Options

    Thank you both. I went with the slightly simpler option from @MichelleH and it does the job. Not sure if adding "upper" to enforce case sensitivity would be more accurate but I will see how the data looks once I analyse it in more details.

    If I wanted to add an extra comparison with "field3" into the mix, can I throw an "OR" in there somewhere? So the outcomes would be "matches field2" "matches field 3" or "looks bad"?

  • Sean_Tully
    Sean_Tully Contributor
    Answer ✓
    Options

    The upper function just helps standardize the strings so that they'll match even if they have different cases within the data.

    For "matches field 3" you would just add another line to identify them:

    case when instr(upper(`field2`), upper(`field1`)) > 0 then 'matches field 2'

    when instr(upper(`field3`), upper(`field1`)) > 0 then 'matches field 3'

    else 'Looks bad'

  • david_cunningham
    edited May 22
    Options

    @Mark Puddephatt - just to second what @Sean_Tully is saying, it's part of my standard practice to wrap text strings in UPPER() before comparing. It just helps to minimize any chance of variability.

    String != string != STRING

    David Cunningham

    ** Was this post helpful? Click Agree 😀, Like 👍️, or Awesome ❤️ below **
    ** Did this solve your problem? Accept it as a solution! ✔️**