SQL Server queries case sensitivity

◇◆丶佛笑我妖孽 提交于 2019-12-30 02:43:31

问题


I have this database:

abcDEF

ABCdef

abcdef

if I write: select * from MyTbl where A='ABCdef'

how to get: ABCdef

and how to get:

abcDEF

    ABCdef

    abcdef

Thanks in advance

forgot to write - sqlCE


回答1:


You can make your query case sensitive by making use of the COLLATE keyword.

SELECT A 
FROM MyTbl 
WHERE A COLLATE Latin1_General_CS_AS = 'ABCdef'



回答2:


If you have abcDEF, ABCdef, abcdef already in the database then it's already case sensitive or you have no constraint.

You'd have to add a COLLATE on both sides to make sure it's truly case sensitive (for a non case sensitive database) which will invalidate index usage

SELECT TheColumn
FROM MyTable 
WHERE TheColumn COLLATE Latin1_General_CS_AS = 'ABCdef' COLLATE Latin1_General_CS_AS

What about accents too? Latin1_General_CS_AI, Latin1_General_Bin?




回答3:


It's all about collation. Each one has a suffix (CI and CS, meaning Case Insensitive, and Case Sensitive).

http://www.databasejournal.com/features/mssql/article.php/10894_3302341_2/SQL-Server-and-Collation.htm




回答4:


SQL is non-case-sensitive by default, so you will get all three items if doing a simple string comparison. To make it case-sensitive, you can cast the value of the field and your search value as varbinary:

SELECT * FROM MyTbl WHERE CAST(A AS varbinary(20)) = CAST('ABCdef' as varbinary(20))

The above assumes your varchar field is sized at 20. For nvarchar double it (thanks @ps2goat).




回答5:


Try this just add binary keyword after where:

select * from MyTbl where binary A = 'ABCdef';


来源:https://stackoverflow.com/questions/3387378/sql-server-queries-case-sensitivity

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!