如何在WHERE子句中进行区分大小写的搜索(我使用的是SQL Server)?

时间:2009-12-02 06:58:41

标签: sql sql-server

我想在SQL查询中进行区分大小写的搜索。但默认情况下,SQL Server不考虑字符串的情况。

如何在SQL查询中进行区分大小写的搜索?

9 个答案:

答案 0 :(得分:153)

通过使用归类或转换为二进制,如下所示:

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 

存在用户名/密码的重复,以使引擎有可能使用索引。上面的排序规则是区分大小写的排序规则,如有必要,请更改为您需要的排序规则。

第二个,转换为二进制,可以这样做:

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 

答案 1 :(得分:130)

可以通过changing the Collation完成。默认情况下,它不区分大小写。

摘自链接:

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

或者,change the columns to be case sensitive

答案 2 :(得分:13)

您可以使用convert to varbinary进行查询 - 这很容易。 例如:

Select * from your_table where convert(varbinary, your_column) = convert(varbinary, 'aBcD') 

答案 3 :(得分:7)

使用BINARY_CHECKSUM

SELECT 
FROM Users
WHERE   
    BINARY_CHECKSUM(Username) = BINARY_CHECKSUM(@Username)
    AND BINARY_CHECKSUM(Password) = BINARY_CHECKSUM(@Password)

答案 4 :(得分:5)

使用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

...在where子句中

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

或找到一个值

declare @value_b nvarchar(1) = 'A'

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

-- output:
-- A = A

答案 5 :(得分:4)

在您的sql db

中使用Latin1_General_CS作为排序规则

答案 6 :(得分:3)

在MySQL中,如果您不想更改排序规则并想要执行区分大小写的搜索,那么只需使用二进制关键字,如下所示:

SELECT * FROM table_name WHERE binary username=@search_parameter and binary password=@search_parameter

答案 7 :(得分:0)

DECLARE @TB TABLE(NAME VARCHAR(100));
INSERT INTO @TB VALUES ('Red'),('Red'),('Green'),('Blue'),('White'),('White')
--**Delete by Rank**
;WITH CTE AS(SELECT NAME,DENSE_RANK() OVER (PARTITION BY NAME ORDER BY NEWID()) ID FROM @TB)
DELETE FROM CTE WHERE ID>1
SELECT NAME FROM @TB;
--**Delete by Row Number** 
;WITH CTE AS(SELECT NAME,ROW_NUMBER() OVER (PARTITION BY NAME ORDER BY NAME) ID FROM @TB)
DELETE FROM CTE WHERE ID>1;
SELECT NAME FROM @TB;

答案 8 :(得分:-3)

就像其他人所说的,您可以执行区分大小写的搜索。或者只是像我一样更改指定列的排序规则格式。对于我数据库中的“用户/密码”列,我通过以下命令将它们更改为排序规则:

ALTER TABLE `UserAuthentication` CHANGE `Password` `Password` VARCHAR(255) CHARACTER SET latin1 COLLATE latin1_general_cs NOT NULL;