Advent of Code is back! Unwrap daily challenges to sharpen your Alteryx skills and earn badges along the way! Learn more now.
Community is experiencing an influx of spam. As we work toward a solution, please use the 'Notify Moderator' option on the ellipsis menu to flag inappropriate posts.
Free Trial

Alteryx Designer Desktop Discussions

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

Split Delimited String on nth delimiter, in Database Formula

warrencowan
9 - Comet

Hi All,

 

Im experimenting with the in database tools as a way to improve my processing speeds and its very effective, however in previous tools I have used I would have used a simple split expression to extract information from fields to new columns eg split([field], ' ' , 2]) , and I know there's a good split columns fucntion in the in memeory toolset, but the in database formula tools require a SQL statement.

 

I've gone through countless examples of varying and mind dboggling complexity (one guys written an impressive thesis on the topic)  but can't make anything work.

 

Anyone have any solutions to writing a SQL statement to split the below string on the 3rd under score

 

So that my current column value of 'product code': 123_3456_78910_45_45_bvAGSBD6252-BT56383

 

can generate the new columns of short code: 123_3456_78910

 

 

Many thanks in advance

2 REPLIES 2
Claje
14 - Magnetar

This SQL statement is somewhat awkward and probably not the most efficient way to accomplish what you want, but it is functional in cases where you have 3 underscores.

 

You can create your first column using the following Formula In-DB:

LEFT("product code",CHARINDEX('_',"product code",CHARINDEX('_',"product code",CHARINDEX('_',"product code")+1)+1)-1)

CHARINDEX() lets you find the first instance of a certain character in a field, and returns its position.  So if you nest CHARINDEX() formulas, using a +1 to increment PAST the last underscore we found, you can find the next one.  Then in the LEFT() statement to pull only the left side, you can subtract one character to remove the final underscore.

 

Assuming you name this column "short code", you can create the second column using the following formula.  You will need a second Formula In-DB tool to do this.

 

RIGHT("product code",LEN("product code")-LEN("short code"))

 

warrencowan
9 - Comet

Boom. it works.

 

Thnx for that, and the explanation. The + - bit was really throwing me.

Labels
Top Solution Authors