Can anyone explain the rules for how a wildcard character range, eg [A-D], works with a case-sensitive collation?
I would have thought the following
It can be done in either way:
a. Use COLLATE
while create the table as:
CREATE TABLE #TEST_LIKE_Patterns
(
ID INT IDENTITY(1,1),
CharColumn VARCHAR(100) COLLATE Latin1_General_BIN
);
b. Use COLLATE
while selecting data as
SELECT *
FROM #TEST_LIKE_Patterns
WHERE CharColumn LIKE '%[A-D]%' COLLATE Latin1_General_BIN;
You need a binary collation as indicated in Md. Elias Hossain's answer.
The explanation is that ranges in the pattern syntax work off Collation sort order rules.
From BOL
In range searches, the characters included in the range may vary depending on the sorting rules of the collation.
So
;WITH T(C) AS
(
SELECT 'A' UNION ALL
SELECT 'B' UNION ALL
SELECT 'C' UNION ALL
SELECT 'D' UNION ALL
select 'a' union all
select 'b' union all
select 'c' union all
select 'd'
)
SELECT *
FROM T
ORDER BY C COLLATE Latin1_General_CS_AS
Returns
C
----
a
A
b
B
c
C
d
D
So the range A-D
excludes a
but includes the other 3 lower case letters under a CS
collation.
Using a case-sensitive collation works for search strings that are not in a range e.g. this would work:
SELECT *
FROM #TEST_LIKE_Patterns
WHERE (
CharColumn LIKE 'A%' COLLATE Latin1_General_CS_AS
OR CharColumn LIKE 'B%' COLLATE Latin1_General_CS_AS
OR CharColumn LIKE 'C%' COLLATE Latin1_General_CS_AS
OR CharColumn LIKE 'D%' COLLATE Latin1_General_CS_AS
);
...but clearly that's not an acceptable approach!
As others have suggested, use Latin1_General_BIN
for ranges.
try
SELECT *
FROM #TEST_LIKE_Patterns
WHERE CharColumn LIKE '[A-D]%' COLLATE Latin1_General_BIN;