Free Trial

Alteryx Designer Desktop Discussions

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

How to use the formula icon to check specifically the third character of a string?

sehgaln
8 - Asteroid

I am trying to use the 'if' expression under the formula tool, but can't figure out how to check the third character of a string

18 REPLIES 18
Kenda
16 - Nebula
16 - Nebula

Hey @sehgaln!

 

I'm not sure exactly what you're looking to do with your if statement, but the following expression will return the third character of your string.

REGEX_Replace([Field1], "(..)(.)(.*)", "$2")

Hope this helps!

 

sehgaln 3rd char.PNG

sehgaln
8 - Asteroid

@BarnesK

If I need to do something like this: if third character in string is equal to 'X' then....

Would I still use RegEx?

Kenda
16 - Nebula
16 - Nebula

Yes @sehgaln. Here is a modified example from my first reply:

iif(REGEX_Replace([Field1], "(..)(.)(.*)", "$2")="c","TRUE","FALSE")

This looks to see if the third character is 'c' and returns TRUE if so or FALSE if not.

I added some spaces in the data to show that it still works.

sehgaln 3rd char2.PNG

 

sehgaln
8 - Asteroid

This helps! What if I don't want it to say True or False? 

 

I want it so that if the third character is 'c' then set column to null

Kenda
16 - Nebula
16 - Nebula

@sehgaln See if this gets you what you're wanting:

 

iif(REGEX_Replace([Field1], "(..)(.)(.*)", "$2")="c",null(),[Field1])

This checks if the third character is 'c' and makes the value null if so or back to the original value if not.

sehgaln
8 - Asteroid

Yes, that is close! Thank you :)

MarqueeCrew
20 - Arcturus
20 - Arcturus

@sehgaln,

 

An alternative approach (@LordNeilLord, you might like this) to @Kenda's award-winning solution:

 

iif(Substring([Field1],2,1)="C",Null(),[Field1])

Instead of using a regular expression (pattern matching), if you use this string function you will get the same results.  There are always multiple ways to solve a challenge with Alteryx.  I chose this method because your search in position 3 is constant.  The substring function will run Faster than the Regex function.  I tested with 2 million rows and it solves the problem in less than 1/2 of the time (3.7 vs 1.5 seconds).

 

Cheers,

Mark

Alteryx ACE & Top Community Contributor

Chaos reigns within. Repent, reflect and restart. Order shall return.
Please Subscribe to my youTube channel.
sehgaln
8 - Asteroid

Thank you! How would I do multiple iif statements in the expression? Not nested iif, just multiple?

MarqueeCrew
20 - Arcturus
20 - Arcturus

@sehgaln,

 

More information in the question would be helpful.

 

iif(
Substring([Field1],2,1)="C" or Substring([Field1],4,1)="C"
,Null(),[Field1])

that's one way to have multiple conditions that end in the same result.

 

As a style, I generally have 1 condition in an IIF statement (2 is the exception).  If you explain the ask with a little more detail, I'd be happy to give you a quick response.

 

Cheers,

Mark

Alteryx ACE & Top Community Contributor

Chaos reigns within. Repent, reflect and restart. Order shall return.
Please Subscribe to my youTube channel.
Labels
Top Solution Authors