How to compare strings based on ASCII code in SSIS

there were two thread in this week which has issue when compare two strings in SSIS Expressions,

for example request was "9:9" < "99" .

if you try the above expression in SSIS, you will get True as result.

this may seems OK, but requests was to compare them by ASCII character, if you look at ASCII table, you will find that ascii("9") is 57, but ascii(":") is 58 . This means that if you compare these two string in ascii order, result should be "9:9" is greater than "99".

So there are two questions:

1- what sort order is used in string comparison in SSIS expressions?

2- how you can compare two string based on ASCII order?

answeres:

1- when you compare two string in SSIS expression, this will compare them by String SORT method. and this will sort nonalphanumerical characters first, and then alphanumerical characters. in this sorting option ":" comes before "9". so if your compare "9:9" with "99", result is : "9:9" is lower than "99" .

 this is MSDN evidence:

In a string sort, the hyphen and the apostrophe, as well as other nonalphanumeric symbols, come before alphanumeric characters.

2- for compare two string based on ASCII codes, use Ordinal compare option. 

Ordinal Compare Option as MSDN defined:

Indicates that the string comparison must use successive Unicode
UTF-16 encoded values of the string (code unit by code unitcomparison),
leading to a fast comparison but one that is culture-insensitive. A
string starting with a code unit XXXX16 comes before a string starting with YYYY16, if XXXX16 is less than YYYY16. This value cannot be combined with other CompareOptions values and must be used alone.

so if you write this .NET code:

string.Compare("9:9", "99", new
System.Globalization.CultureInfo("en-US"),
System.Globalization.CompareOptions.Ordinal).ToString();

 this will result 1, means that "9:9" is greater than "99".

 Note that you should write this code in Script component to compare strings by ascii order.

for more information about other compare options of string.compare method look here .

Reza Rad on FacebookReza Rad on LinkedinReza Rad on TwitterReza Rad on Youtube
Reza Rad
Trainer, Consultant, Mentor
Reza Rad is a Microsoft Regional Director, an Author, Trainer, Speaker and Consultant. He has a BSc in Computer engineering; he has more than 20 years’ experience in data analysis, BI, databases, programming, and development mostly on Microsoft technologies. He is a Microsoft Data Platform MVP for 12 continuous years (from 2011 till now) for his dedication in Microsoft BI. Reza is an active blogger and co-founder of RADACAD. Reza is also co-founder and co-organizer of Difinity conference in New Zealand, Power BI Summit, and Data Insight Summit.
Reza is author of more than 14 books on Microsoft Business Intelligence, most of these books are published under Power BI category. Among these are books such as Power BI DAX Simplified, Pro Power BI Architecture, Power BI from Rookie to Rock Star, Power Query books series, Row-Level Security in Power BI and etc.
He is an International Speaker in Microsoft Ignite, Microsoft Business Applications Summit, Data Insight Summit, PASS Summit, SQL Saturday and SQL user groups. And He is a Microsoft Certified Trainer.
Reza’s passion is to help you find the best data solution, he is Data enthusiast.
His articles on different aspects of technologies, especially on MS BI, can be found on his blog: https://radacad.com/blog.

Leave a Reply