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
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
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;