简体   繁体   English

如何在数据库中查询区分大小写的值

[英]How to query database for case sensitive values

I'm checking a few values in my database and it appears that the strings being compared are not being case sensitive enforced. 我正在检查数据库中的一些值,似乎正在比较的字符串不区分大小写。 I'm using C# and LINQ to SQL. 我正在使用C#和LINQ to SQL。

You do not need to change your database you can modify your queries to work case sensitive: 您不需要更改数据库,可以修改查询以区分大小写:

SET NOCOUNT ON
declare @curdb sysname; select  @curdb =  db_name(0);
select  DATABASEPROPERTYEX(@curdb, 'Collation') Collation;

if object_id('test_CI', 'U') is not null
    drop table test_CI
go

create table test_CI (
    val varchar(10)
    );
go
insert into test_CI values ('TEST');    
insert into test_CI values ('Test');    
insert into test_CI values ('test');    

-- 
select * from test_CI where val = 'Test';

select * from test_CI where val collate Latin1_General_CS_AS = 'Test';

Just use your current sort order and replace the CI by CS. 只需使用当前的排序顺序,然后用CS替换CI即可。

In SQL Server, whether a column is case sensitive or not depends on what collation you've applied to it. 在SQL Server中,列是否区分大小写取决于您对其应用的排序规则。

The default collation is likely "SQL_Latin1_General_CP1_CI_AS" (CI being "case insensitive"). 默认排序规则可能是“ SQL_Latin1_General_CP1_CI_AS”(CI为“不区分大小写”)。 If you change your database schema so that column has the collation "SQL_Latin1_General_CP1_CS_AS" instead, then queries on it will be case-sensitive. 如果更改数据库架构,以使该列具有排序规则“ SQL_Latin1_General_CP1_CS_AS”,则对它的查询将区分大小写。

Check the collation matrix provided here: 检查此处提供的整理矩阵:

http://msdn.microsoft.com/en-us/library/ms144250.aspx http://msdn.microsoft.com/en-us/library/ms144250.aspx

I think you will need something like SQL_Latin1_General_Cp437_CS_AS_KI_WI 我认为您将需要像SQL_Latin1_General_Cp437_CS_AS_KI_WI

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

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