We are celebrating the 10-year anniversary of the Alteryx Community! Learn more and join in on the fun here.
Start Free Trial

Alteryx Designer Desktop Discussions

Find answers, ask questions, and share expertise about Alteryx Designer Desktop and Intelligence Suite.
SOLVED

Conditional Statement based on Nth Characters in a string

pacbloyd
7 - Meteor

Hello - I need to write conditional statements based on certain characters in a string.

 

For example, If the 1st character is "D" AND 4th Character is L or S or F or Z THEN "Kansas"

 

Also something similar but 

 

If the 4th character is "D" AND 6th Character is L or S or F or Z THEN "Kansas"

 

How would I write this?

 

Thank you!

6 REPLIES 6
Jonathan-Sherman
15 - Aurora
15 - Aurora

Hi @pacbloyd,

 

You could use:

 

IF StartsWith([String], 'D') AND Substring([String],3,1) IN('L','S','F','Z')
THEN 'Kansas' ELSE ''
ENDIF

 

JonathanSherman_0-1641330674831.png

 

I've attached my workflow for you to download if needed!

 

Kind regards,

Jonathan

pacbloyd
7 - Meteor

Thanks @Jonathan-Sherman, and what about a case where the conditional depended on the 4th character? I assume StartsWith won't work?

Jonathan-Sherman
15 - Aurora
15 - Aurora

It's worth noting the SubString() function indexes from 0, so to search the 4th character you'll want to start from 3 rather than 4, can catch people out!

Jonathan-Sherman
15 - Aurora
15 - Aurora

Could you give an example?

pacbloyd
7 - Meteor

Sure - we can use the same letters, but say it depends on what the 4th character is? I edited my OP to include an example

Jonathan-Sherman
15 - Aurora
15 - Aurora

You could just go with two SubString() functions:

 

IF Substring([String],3,1) = 'D' AND Substring([String],5,1) IN('L','S','F','Z')
THEN 'Kansas' ELSE ''
ENDIF

 

Kind regards,

Jonathan

Labels
Top Solution Authors