简体   繁体   English

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

[英]How to do a case sensitive search in WHERE clause (I'm using SQL Server)?

I want to do a case sensitive search in my SQL query.我想在我的 SQL 查询中进行区分大小写的搜索。 But by default, SQL Server does not consider the case of the strings.但默认情况下,SQL Server 不考虑字符串的大小写。

Any idea on how to do a case sensitive search in SQL query?关于如何在 SQL 查询中进行区分大小写搜索的任何想法?

Can be done via changing the Collation .可以通过更改 Collat​​ion来完成。 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 CustPassword = @CustPassword COLLATE SQL_Latin1_General_CP1_CS_AS

Or, change the columns to be case sensitive .或者, 将列更改为区分大小写

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 

You can make the query using convert to varbinary – it's very easy.您可以使用 convert to varbinary 进行查询 - 这非常简单。 Example:例子:

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

USE BINARY_CHECKSUM使用 BINARY_CHECKSUM

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

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 ...在 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

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

在 sql db 中使用 Latin1_General_CS 作为排序规则

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

SELECT * FROM table_name WHERE binary username=@search_parameter and binary password=@search_parameter
select * from incidentsnew1 
where BINARY_CHECKSUM(CloseBy) = BINARY_CHECKSUM(Upper(CloseBy))

You can do by simply altering column collation as您可以通过简单地将列排序规则更改为

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

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请参阅: https://learn.microsoft.com/en-us/ef/core/miscellaneous/collations-and-case-sensitivity#explicit-collation-in-a-query

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;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM