SQLCLR and Collations

PASS Summit Unite 2009 Speaker Tip

An important quality of a well-written SQL Server Common Language Runtime (SQLCLR) routine is transparency: A caller should never have to think about, or even realize, that a given routine was written in a .NET language rather than in T-SQL. As developers, therefore, our job is to be mindful of the differences between the two systems so that we can create routines that behave the way we—and our end users—expect them to.

One of the most important differences between data in SQL Server and in .NET involves strings and how they are compared. In SQL Server, all strings have an associated collation. The collation controls the behavior of comparison operations, a factor that is considered any time data needs to be sorted, indexed, or otherwise manipulated. The collation used by a given string can be inherited from the parent database or table, depending on the context, or can be defined at runtime using a collation designator. In .NET languages, however, strings don’t have collations. Rather, they have default comparison behaviors that you must explicitly override using a structure called CompareInfo.

How does this difference affect those of us writing solutions using SQLCLR routines? To make a SQLCLR routine behave just like a T-SQL routine, we need to communicate the collation settings of a given string when passing it into the SQLCLR routine as an argument. The solution, luckily, isn’t too difficult: You can use the SqlString structure, found in the System.Data.SqlTypes namespace, as an input parameter to your SQLCLR routines. This structure maps to all the SQL Server string types and includes the CompareInfo property, which exposes a fully-formed instance of the CompareInfo structure based on the collation of the input string.

Read more from Adam about his Nov. 2 PASS Summit 2009 pre-conference seminar, "SQLCLR from Beginner to Expert."


Editor's Note: SQL Server Magazine would like to thank PASS for providing this technical tip.

Please or Register to post comments.

IT/Dev Connections

Las Vegas
September 30th - October 4th

Paul ThurottOur Experts will show you:
• Common SQL Server
Problems
• Best Practices for T-SQL
• SQL Server Integration
Services
• Database Development

Come See Michael Otey & Tim Ford in Person!

Early Registration Now Open

From the Blogs
May 21, 2013
blog

A Common Misconception about MAXDOP

Out of the box, SQL Server is (and has been) able to take advantage of multiple processors/cores without any effort on behalf of administrators....More
May 9, 2013
blog

My ISO 8601-Compliant Signature 2

My family recently just "officially" announced that we're in the process of adopting a child from South Africa. We're quite excited, of course, but there's a ton of paperwork to do—along with the need for gobs of signatures....More
May 8, 2013
blog

Use SSIS for ETL from Hadoop

In this blog post, Mark Kromer walks you through using SSIS as a way to use ETL techniques using Microsoft's Hadoop on Windows (HDInsight) as a source using Hive connectors...More
SQL Server Pro Forums

Get answers to questions, share tips, and engage with the SQL Server community in our Forums.