312

How do you compare strings so that the comparison is true only if the cases of each of the strings are equal as well. For example:

Select * from a_table where attribute = 'k'

...will return a row with an attribute of 'K'. I do not want this behaviour.

2
  • 2
    It might not be what you need but you can change the Collation or use a specific Collation in your query.
    – Kane
    Oct 19, 2010 at 13:58
  • 10
    Which SQL product?
    – onedaywhen
    Oct 20, 2010 at 9:23

6 Answers 6

502
Select * from a_table where attribute = 'k' COLLATE Latin1_General_CS_AS 

Did the trick.

9
  • 8
    I'd normally use Latin1_General_Bin
    – gbn
    Oct 19, 2010 at 17:53
  • 3
    Yes, the Standard approach is to use a case-insensitive collation, though the collations themselves are vendor-specific. Is yours SQL Server syntax?
    – onedaywhen
    Oct 20, 2010 at 9:24
  • 1
    In my case, I have 1 column in my db that is case-sensitive. I needed to compare it to a standard (CI) column. I used a variation of this WHERE Foo.Bar = (Baz.Bar COLLATE Latin1_General_CS_AS)
    – Hypnovirus
    Jun 5, 2014 at 15:41
  • 3
    Thanks but what is Latin1_General_CS_AS ?? Is it special keyword? Apr 2, 2015 at 4:55
  • 3
    @VijaySinghRana Latin1_General_CS_AS is a specification of the collation. Collation refers to a set of rules that determine how data is sorted and compared. See this page for more information.
    – amccormack
    Apr 3, 2015 at 1:50
61

You can also convert that attribute as case sensitive using this syntax :

ALTER TABLE Table1
ALTER COLUMN Column1 VARCHAR(200)
COLLATE SQL_Latin1_General_CP1_CS_AS

Now your search will be case sensitive.

If you want to make that column case insensitive again, then use

ALTER TABLE Table1
ALTER COLUMN Column1 VARCHAR(200)
COLLATE SQL_Latin1_General_CP1_CI_AS
1
  • I have nvarchar type column. Above query didn't work for me. Any reason? Sep 21, 2021 at 3:21
31

You Can easily Convert columns to VARBINARY(Max Length), The length must be the maximum you expect to avoid defective comparison, It's enough to set length as the column length. Trim column help you to compare the real value except space has a meaning and valued in your table columns, This is a simple sample and as you can see I Trim the columns value and then convert and compare.:

CONVERT(VARBINARY(250),LTRIM(RTRIM(Column1))) = CONVERT(VARBINARY(250),LTRIM(RTRIM(Column2)))

Hope this help.

0
23

Just as another alternative you could use HASHBYTES, something like this:

SELECT * 
FROM a_table 
WHERE HASHBYTES('sha1', attribute) = HASHBYTES('sha1', 'k')
3
  • 3
    What about collisions? It'd be rare but I assume there would be multiple strings that hash to the same value. Nov 23, 2018 at 7:43
  • Yes possible, but extremely rare on such a simple string example I would have thought. Nov 23, 2018 at 8:49
  • 3
    @DavidKlempfner why not first do the compare and if they match then also check for hashbytes? We could make this a function and invoke it like StringsAreCaseSensitiveEqual(a, b) => a = b AND HASHBYTES('sha1', a) = HASHBYTES('sha1', b) Dec 17, 2019 at 9:26
11

simplifying the general answer

SQL Case Sensitive String Compare

These examples may be helpful:

Declare @S1 varchar(20) = 'SQL'
Declare @S2 varchar(20) = 'sql'

  
if @S1 = @S2 print 'equal!' else print 'NOT equal!' -- equal (default non-case sensitivity for SQL

if cast(@S1 as binary) = cast(Upper(@S2) as binary) print 'equal!' else print 'NOT equal!' -- equal

if cast(@S1 as binary) = cast(@S2 as binary) print 'equal!' else print 'NOT equal!' -- not equal

if  @S1 COLLATE Latin1_General_CS_AS  = Upper(@S2) COLLATE Latin1_General_CS_AS  print 'equal!' else print 'NOT equal!' -- equal

if  @S1 COLLATE Latin1_General_CS_AS  = @S2 COLLATE Latin1_General_CS_AS  print 'equal!' else print 'NOT equal!' -- not equal

 

The convert is probably more efficient than something like runtime calculation of hashbytes, and I'd expect the collate may be even faster.

3

You can define attribute as BINARY or use INSTR or STRCMP to perform your search.

1
  • This reply doesn't seem to be about SQL Server per the question tag. That DBMS lacks these INSTR and STRCMP functions.
    – Jonas
    Dec 4, 2019 at 11:11

Not the answer you're looking for? Browse other questions tagged or ask your own question.