How to compare strings based on ASCII code in SSIS

FacebooktwitterredditpinterestlinkedintumblrmailFacebooktwitterredditpinterestlinkedintumblrmail
FacebooktwitterlinkedinrssyoutubeFacebooktwitterlinkedinrssyoutube

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 .

FacebooktwitterredditpinterestlinkedintumblrmailFacebooktwitterredditpinterestlinkedintumblrmail
FacebooktwitterlinkedinrssyoutubeFacebooktwitterlinkedinrssyoutube
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 nine 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.
His articles on different aspects of technologies, especially on MS BI, can be found on his blog: https://radacad.com/blog.
He wrote some books on MS SQL BI and also is writing some others, He was also an active member on online technical forums such as MSDN and Experts-Exchange, and was a moderator of MSDN SQL Server forums, and is an MCP, MCSE, and MCITP of BI. He is the leader of the New Zealand Business Intelligence users group. He is also the author of very popular book Power BI from Rookie to Rock Star, which is free with more than 1700 pages of content and the Power BI Pro Architecture published by Apress.
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.

Leave a Reply