问题
I just realized that SQL server '=' comparator when used for text comparison is case insensitive. I have a few questions regarding this functionality:
- Is this the same for all databases or specific to SQL server?
- I have been using the
lowerfunction to ensure the text comparison is insensitive till now. Is it still a good idea to follow the same? - How can we do case sensitive comparisons in SQL server?
- 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