So, you have a column with some upper case and lower case values. By default, SQL Server treats upper case and lower case the same. The case sensitivity can be set at the Server, Database, or Column level as well as on a query by query basis...
The keywords to be used in case sensitive queries are "COLLATE sql_latin1_general_cp1_cs_as" and are used in the following ways:
DECLARE @myTable TABLE (
Column1 VARCHAR(1),
Column2 INT);
INSERT INTO @myTable VALUES ('a',1),('a',1),('A',1),('b',1),('B',1)
--Case Sensitive Grouping
SELECT
Column1 COLLATE sql_latin1_general_cp1_cs_as AS CaseSensitiveColumn1,
SUM(Column2) AS Column2
FROM
@myTable
GROUP BY
Column1 COLLATE sql_latin1_general_cp1_cs_as
--Case Sensitive Case When Statement
SELECT
Column1,
Column2,
CASE
WHEN Column1 = 'A' COLLATE sql_latin1_general_cp1_cs_as
THEN 'Yes'
ELSE 'No'
END AS IsUpperCaseA
FROM
@myTable
--Case Sensitive Where Clause
SELECT
Column1,
Column2
FROM
@myTable
WHERE
Column1 = 'a' COLLATE sql_latin1_general_cp1_cs_as
Thanks to http://stackoverflow.com/a/1411260/1582172
No comments:
Post a Comment
Please only include comments that add to the substance of the Blog Post:
- Question or request for clarification
- Idea for improvement
- Identifying an issue
Please refrain from promotion.