Why is SQL Server '=' comparator case insensitive?

允我心安 提交于 2019-12-03 14:59:09

问题


I just realized that SQL server '=' comparator when used for text comparison is case insensitive. I have a few questions regarding this functionality:

  1. Is this the same for all databases or specific to SQL server?
  2. I have been using the lower function to ensure the text comparison is insensitive till now. Is it still a good idea to follow the same?
  3. How can we do case sensitive comparisons in SQL server?
  4. Why is '=' operator defaulting to case insensitive comparison?

回答1:


No, case sensitivity has nothing to do with the equals sign.

Case sensitivity is determined by the collation for the database -- see the documentation for details.




回答2:


Case sensitivity depends only on the collation. You can specify the collation within each '=' operation

SELECT  *
  FROM  [Table_1] a inner join
        [Table_2] b on a.Col1=b.Col2 collate Modern_Spanish_CS_AI



回答3:


I have been using the lower function to ensure the text comparison is insensitive till now. Is it still a good idea to follow the same?

Absolutely not. You will generally preclude the use of an index if you do this. Plain old = (or < or > or whatever) will either work or not depending on the collating you have chosen. Do not do this "just to be safe". Testing will make sure you've got it right.




回答4:


The case sensitivity of operations in SQL Server is determined during installation when you set the collation for the database. At that point in time you can choose to install SQL Server as case insensitive (default) or case sensitive.

http://msdn.microsoft.com/en-us/library/aa197951(v=sql.80).aspx




回答5:


How the comparison is done depends on the collation that you have chosen for the field. If you change the field to use a case sensetive collation, the comparisons will be case sensetive.

By default fields use the collation set for the database, but each field can have it's own collation setting.



来源:https://stackoverflow.com/questions/4938924/why-is-sql-server-comparator-case-insensitive

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