Case sensitivity goes crazy

喜你入骨 提交于 2019-12-10 14:40:55

问题


I have a database and I am trying to execute the following query:

SELECT COUNT(*) FROM [Resource] WHERE Name LIKE 'ChinaApp%'

SELECT COUNT(*) FROM [Resource] WHERE Name LIKE 'Chinaapp%'

This is returning 2 different counts:

The first thing that came to my mind is to check the case sensitivity. I checked the collation on the server level, the database level and the column level:

Server level : Latin1_General_CI_AS

SELECT SERVERPROPERTY('COLLATION')

Database level : Danish_Norwegian_CI_AS

SELECT DATABASEPROPERTYEX('Data Warehouse', 'Collation')

Column level : Danish_Norwegian_CI_AS

SELECT TABLE_NAME, COLUMN_NAME, COLLATION_NAME
FROM INFORMATION_SCHEMA.COLUMNS
WHERE TABLE_NAME = 'Resource'
AND COLUMN_NAME = 'Name'

Question :

What is going wrong with the query? The case sensitivity is disabled as proven before. Why the counts are different?


回答1:


Try this:

SELECT COUNT ( * ) FROM [RESOURCE] WHERE Name COLLATE SQL_Latin1_General_CI_AS LIKE 'Chinaapp%'



回答2:


Danish_Norwegian_CI_AS is the issue! Thank you @realspirituals for the hint!

In this default collation I have, 'aa' is actually one single character. The last line in the following link explain it. Å, å, AA, Aa and aa are all the same.

Collation chart for Danish_Norwegian_CI_AS

The following queries now provide the correct result set (count):

SELECT COUNT(*) FROM [Resource] WHERE Name LIKE 'ChinaApp%'

and

SELECT COUNT(*) FROM [Resource] WHERE Name LIKE 'Chinaapp%'
COLLATE Latin1_General_CI_AS


来源:https://stackoverflow.com/questions/18956099/case-sensitivity-goes-crazy

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