208

I want to do a case sensitive search in my SQL query. But by default, SQL Server does not consider the case of the strings.

Any idea on how to do a case sensitive search in SQL query?

0

12 Answers 12

259

Can be done via changing the Collation. By default it is case insensitive.

Excerpt from the link:

SELECT 1
FROM dbo.Customers
WHERE   CustID = @CustID COLLATE SQL_Latin1_General_CP1_CS_AS
AND     OrderID = @OrderID COLLATE SQL_Latin1_General_CP1_CS_AS

Or, change the columns to be case sensitive.

You can also apply COLLATE to LIKE to make it case sensitive - e.g.,

SELECT    *
FROM     tbl_Partners
WHERE    PartnerName COLLATE SQL_Latin1_General_CP1_CS_AS LIKE 'MyEx%' COLLATE SQL_Latin1_General_CP1_CS_AS
3
  • 3
    How to use when we have in instead =. like WHERE CustID in (@CustID)
    – rinuthomaz
    Sep 27, 2017 at 8:25
  • Collation works for most cases, but if you've got other language characters in your data, it will return false positives: /schwarz-weiß versus: /schwarz-weiss
    – Lazlow
    Jul 23, 2019 at 9:14
  • 2
    applies to LIKE clause ?
    – Kiquenet
    Jul 9, 2021 at 9:37
175

By using collation or casting to binary, like this:

SELECT *
FROM Users
WHERE   
    Username = @Username COLLATE SQL_Latin1_General_CP1_CS_AS
    AND Password = @Password COLLATE SQL_Latin1_General_CP1_CS_AS
    AND Username = @Username 
    AND Password = @Password 

The duplication of username/password exists to give the engine the possibility of using indexes. The collation above is a Case Sensitive collation, change to the one you need if necessary.

The second, casting to binary, could be done like this:

SELECT *
FROM Users
WHERE   
    CAST(Username as varbinary(100)) = CAST(@Username as varbinary))
    AND CAST(Password as varbinary(100)) = CAST(@Password as varbinary(100))
    AND Username = @Username 
    AND Password = @Password 
6
  • 14
    People reading this question may also find it useful to read how to change the column itself to be case sensitive which eliminates the need for using collation in the WHERE clause. See: stackoverflow.com/a/485394/908677 Oct 29, 2015 at 13:00
  • 2
    The cast as varbinary method worked for me when used directly on the database, but did not work when sending the same statement from a .NET application -- no idea why. But the collate method worked fine.
    – Doug
    Sep 20, 2016 at 17:44
  • 1
    This answer would be perfect if it included an explanation of where to put the term searched for, i.e. where the phrase similar to a regular like "*word or phrase*" SQL search would be inserted.
    – Canned Man
    Nov 10, 2016 at 12:24
  • @CannedMan - You can use the above collate solution the same way with the LIKE statement. Simply do the following to return all upper case 'D's. "SELECT * FROM SomeTable WHERE ColumnName like '%D%' COLLATE SQL_Latin1_General_CP1_CS_AS"
    – Radderz
    Dec 12, 2016 at 12:19
  • It doesn't work with czech alphabet. Tested word: 'ukázka'. It is in the table as a singe word in a column, but your search didn't find it. May 22, 2018 at 13:32
15

You can make the query using convert to varbinary – it’s very easy. Example:

Select * from your_table where convert(varbinary, your_column) = convert(varbinary, 'aBcD') 
1
  • 2
    It doesn't work with czech alphabet. Tested word: 'ukázka'. It is in the table as a singe word in a column, but your search didn't find it. May 22, 2018 at 13:30
7

USE BINARY_CHECKSUM

SELECT 
FROM Users
WHERE   
    BINARY_CHECKSUM(Username) = BINARY_CHECKSUM(@Username)
    AND BINARY_CHECKSUM(Password) = BINARY_CHECKSUM(@Password)
2
  • 3
    Wouldn't this mean, that it's no longer an exact comparison? There might be sometimes that it returns true that they aren't actually the same?
    – O'Rooney
    Jul 20, 2015 at 2:42
  • 4
    I agree @O'Rooney this will on occasion return false positives. Dec 18, 2015 at 3:04
6

use HASHBYTES

declare @first_value nvarchar(1) = 'a'
declare @second_value navarchar(1) = 'A'

if HASHBYTES('SHA1',@first_value) = HASHBYTES('SHA1',@second_value) begin
    print 'equal'
end else begin
    print 'not equal'
end

-- output:
-- not equal

...in where clause

declare @example table (ValueA nvarchar(1), ValueB nvarchar(1))

insert into @example (ValueA, ValueB)
values  ('a', 'A'),
        ('a', 'a'),
        ('a', 'b')

select  ValueA + ' = ' + ValueB
from    @example
where   hashbytes('SHA1', ValueA) = hashbytes('SHA1', ValueB)

-- output:
-- a = a

select  ValueA + ' <> ' + ValueB
from    @example
where   hashbytes('SHA1', ValueA) <> hashbytes('SHA1', ValueB)

-- output:
-- a <> A
-- a <> b

or to find a value

declare @value_b nvarchar(1) = 'A'

select  ValueB + ' = ' + @value_b
from    @example
where   hashbytes('SHA1', ValueB) = hasbytes('SHA1', @value_b)

-- output:
-- A = A
0
6

In MySQL if You don't want to change the collation and want to perform case sensitive search then just use binary keyword like this:

SELECT * FROM table_name WHERE binary username=@search_parameter and binary password=@search_parameter
3
  • 3
    That's not a valid SQL Server query. I think that's MySQL
    – Jon Tirjan
    Jul 19, 2017 at 14:33
  • 2
    Works perfectly on MySQL
    – W.M.
    Jul 21, 2017 at 12:04
  • 1
    this works for me in mysql! Thanks! Mar 16, 2022 at 15:36
5

use Latin1_General_CS as your collation in your sql db

2
select * from incidentsnew1 
where BINARY_CHECKSUM(CloseBy) = BINARY_CHECKSUM(Upper(CloseBy))
0
1

You can do by simply altering column collation as

Alter Table UserMaster 
Alter Column Password varchar(50) COLLATE SQL_Latin1_General_CP1_CS_AS
1

use below query to get result with case sensitive.

SELECT * FROM TABLE_NAME WHERE BINARY COLUMN_NAME='YOUR_FILTER'
0

If you are interested in Entity Framework approarch:

var customers = context.Customers
.Where(c => EF.Functions.Collate(c.Name, "SQL_Latin1_General_CP1_CS_AS") == "John")
.ToList();

See: https://learn.microsoft.com/en-us/ef/core/miscellaneous/collations-and-case-sensitivity#explicit-collation-in-a-query

-6

Just as others said, you can perform a case sensitive search. Or just change the collation format of a specified column as me. For the User/Password columns in my database I change them to collation through the following command:

ALTER TABLE `UserAuthentication` CHANGE `Password` `Password` VARCHAR(255) CHARACTER SET latin1 COLLATE latin1_general_cs NOT NULL;
1
  • Do NOT store passwords as plaintext! They should've been hashed and salted, and then the comparison is on the hash and salt! This is simply a terrible answer!
    – Nelson
    Apr 9, 2019 at 4:11

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