开发者

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 开发者_C百科are not being case sensitive enforced. I'm using C# and 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.


In SQL Server, whether a column is case sensitive or not depends on what collation you've applied to it.

The default collation is likely "SQL_Latin1_General_CP1_CI_AS" (CI being "case insensitive"). 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.


Check the collation matrix provided here:

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

I think you will need something like SQL_Latin1_General_Cp437_CS_AS_KI_WI

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜