vrijdag 13 januari 2006

Case sensitive queries in SQL Server

In SQL Server, whether your database is case sensitive or not, depends on the collation that your database uses.

If you have a database that has been setup with a case insensitive collation sequence, but you want to retrieve or sort some data in a case sensitive way, you can do it like this:
SELECT * 
FROM MyTable
WHERE aField = 'a' COLLATE Latin1_General_CS_AS

or, for sorting:
SELECT *
FROM MyTable
ORDER BY aField COLLATE Latin1_General_CS_AS

As you can see, you just have to define a case sensitive (CS stands for Case Sensitive) collation in your query.

Of course, if you have a table that contains data that should be treated in a case-sensitive way, you can create that table/columns with a case sensitive collation sequence.

Geen opmerkingen: