Community Spring Cleaning week is here! Join your fellow Maveryx in digging through your old posts and marking comments on them as solved. Learn more here!

Alteryx Designer Desktop Discussions

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

Converting character to a unique number

MarionD
7 - Meteor

Hello,

 

I need to convert strings (in my case countries) into unique code number.

I tried the function CharToInt, but it gives me identical numbers for different countries (for example 65 for australia, azerbaidjan, austria, argentina etc.).

Can someone explain me why ? Is there a solution to convert a string into a unique code ? 

 

Thank you

11 REPLIES 11
MarqueeCrew
20 - Arcturus
20 - Arcturus

@MarionD,

 

Would you consider using ISO country codes?  You would first standardize the spelling of each country and then use a FIND & REPLACE Tool (you can use a join, but I think that the F&R tool is a better choice) to convert the countries to codes.

 

https://en.wikipedia.org/wiki/ISO_3166-1

Alteryx ACE & Top Community Contributor

Chaos reigns within. Repent, reflect and restart. Order shall return.
Please Subscribe to my youTube channel.
jdunkerley79
ACE Emeritus
ACE Emeritus

CharToInt is returning the first character of the string's ASCII code.

 

While not guarenteed to be unique, you could use the MD5_ASCII function to get the hashcode of the string which may be unique!

 

 

pcatterson
11 - Bolide

CharToInt only looks at the first character of a string in its conversion.  All strings beginning with the same first character will be converted to the same integer.

 

May I suggest a join to accomplish this?  This can codify your countries based on a list from an outside source or based on one that you provide.

MarionD
7 - Meteor

Thank you, I didn't think of ISO code!

MarionD
7 - Meteor

Thank you for the explanation !

I tried the MD-ASCII function, and it returns a code but with letters also!

So I guess there is no function to convert directly a string to a unique code number...

MarqueeCrew
20 - Arcturus
20 - Arcturus

@MarionD,

 

As a bonus, here is a formula:

 

 

tonumber(tostring(chartoint(left(soundex([String]),1)))+right(soundex([String]),3))

It will convert the string into a soundex key (e.g. Canada becomes C520) and then convert the C to 67 and will then put the 67 in front of the 520. 67520 is the result.

Alteryx ACE & Top Community Contributor

Chaos reigns within. Repent, reflect and restart. Order shall return.
Please Subscribe to my youTube channel.
MarionD
7 - Meteor

Great! That will help me a lot to convert other kind of string datas !

 

Thank you :)

jdunkerley79
ACE Emeritus
ACE Emeritus

If you want the MD5 Hash as a number:

Hextonumber(md5_ascii([Field1]))
pcatterson
11 - Bolide

One warning about Mark's soundex approach, if the beginning of the string is too similar, then the numbered code will not be unique.  Dominica and Dominican Republic would both be 71552 and United States and United Kingdom would both be 85532.

Labels