When selecting data in SQL Server, the WHERE-clause has, in my experience, ignored the case and done a case-insensitive comparison. For example, my Individuals table may have a mix of upper and lower cases, depending on how people entered their names:
SELECT firstname, lastname
FROM Individuals
WHERE lastname = 'Timmins'
firstname lastname
--------- --------
Tammy TIMMINS
Tellie timmins
Tommy TimMinS
Tubby Timmins
My query selected all four people with the last name "Timmins" without caring if it was all capitals, all lower case, or a mixture. I rely on that behavior all the time.
But once in a while, we need to do a case-sensitive query. How can we, for example, find Tommy TimMinS and correct it to Tommy Timmins?
The secret is to alter the Collation. This is a setting at the database-level, that controls the sort order within Unicode data types, and within non-Unicode character data types. (see here for an introduction)
To see the current collation setting of your database, in SQL Server Management Studio, right-click on the database and select Properties. The Collation setting lives on the General page, under Maintenance. The one my current project uses is "SQL_Latin1_General_CP1_CI_AS". The "CI" setting indicates that, for this database, order is Case Insensitive.
You can change the Collation property here, but it is a database-level property and there may be very good business and technical reasons it has its current settings.
Fortunately, you can alter the collation just for your query. In this way, you can still do a case-sensitive query in a database whose collation is set to Case-Insensitive collation.
My query above can be modified to select only the entries with the last name "TimMinS" by changing the "CI" for Case-Insensitive to "CS" for Case-Sensitive:
SELECT firstname, lastname
FROM Individuals
WHERE lastname COLLATE SQL_Latin1_General_CP1_CS_AS = 'TimMinS'
firstname lastname
--------- --------
Tommy TimMinS
I can also use this COLLATE key word to tailor the update and correct this value:
UPDATE Individuals
SET lastname = 'Timmins'
WHERE lastname COLLATE Latin1_General_CS_AS = 'TimMinS'
(1 row(s) affected)
Now Tommy's last name should be "Timmins" with that exact case, while the other variants remain untouched. Let's check that, re-running the original query above:
SELECT firstname, lastname
FROM Individuals
WHERE lastname = 'Timmins'
firstname lastname
--------- --------
Tammy TIMMINS
Tellie timmins
Tommy Timmins
Tubby Timmins
There are more options than the two Collation settings I have identified here. Check your SQL Server documentation to find the appropriate one.
SELECT firstname, lastname
FROM Individuals
WHERE lastname = 'Timmins'
firstname lastname
--------- --------
Tammy TIMMINS
Tellie timmins
Tommy TimMinS
Tubby Timmins
My query selected all four people with the last name "Timmins" without caring if it was all capitals, all lower case, or a mixture. I rely on that behavior all the time.
But once in a while, we need to do a case-sensitive query. How can we, for example, find Tommy TimMinS and correct it to Tommy Timmins?
The secret is to alter the Collation. This is a setting at the database-level, that controls the sort order within Unicode data types, and within non-Unicode character data types. (see here for an introduction)
To see the current collation setting of your database, in SQL Server Management Studio, right-click on the database and select Properties. The Collation setting lives on the General page, under Maintenance. The one my current project uses is "SQL_Latin1_General_CP1_CI_AS". The "CI" setting indicates that, for this database, order is Case Insensitive.
You can change the Collation property here, but it is a database-level property and there may be very good business and technical reasons it has its current settings.
Fortunately, you can alter the collation just for your query. In this way, you can still do a case-sensitive query in a database whose collation is set to Case-Insensitive collation.
My query above can be modified to select only the entries with the last name "TimMinS" by changing the "CI" for Case-Insensitive to "CS" for Case-Sensitive:
SELECT firstname, lastname
FROM Individuals
WHERE lastname COLLATE SQL_Latin1_General_CP1_CS_AS = 'TimMinS'
firstname lastname
--------- --------
Tommy TimMinS
I can also use this COLLATE key word to tailor the update and correct this value:
UPDATE Individuals
SET lastname = 'Timmins'
WHERE lastname COLLATE Latin1_General_CS_AS = 'TimMinS'
(1 row(s) affected)
Now Tommy's last name should be "Timmins" with that exact case, while the other variants remain untouched. Let's check that, re-running the original query above:
SELECT firstname, lastname
FROM Individuals
WHERE lastname = 'Timmins'
firstname lastname
--------- --------
Tammy TIMMINS
Tellie timmins
Tommy Timmins
Tubby Timmins
There are more options than the two Collation settings I have identified here. Check your SQL Server documentation to find the appropriate one.