Why doesn't explicit COLLATE override database collation?

南楼画角 提交于 2019-12-11 01:28:25

问题


I am on SQL Server 2008 R2 dev, server default collation is Cyrillic_General_CI_AS

Executing in SSMS
SELECT 'éÉâÂàÀëËçæà' COLLATE Latin1_General_CS_AS
or

SELECT 'éÉâÂàÀëËçæà' COLLATE Latin1_General_CI_AI   

outputs

  • eEaAaAeEc?a on(in ocntext of/use dbName) of database with default collation Cyrillic_General_CI_AS
  • éÉâÂàÀëËçæà on database with default collation Latin1_General_CI_AS

Why?


回答1:


Those character literals in your queries are first converted to varchar strings under whatever collation the database is set for, and then your collation cast takes effect.

If you want to pass such character literals and ensure all characters are faithfully represented, it's better to pass them as nvarchar literals:

create database CollTest collate Cyrillic_General_CI_AS
go
use CollTest
go
SELECT 'éÉâÂàÀëËçæà' COLLATE Latin1_General_CS_AS   
SELECT 'éÉâÂàÀëËçæà' COLLATE Latin1_General_CI_AI
go
SELECT N'éÉâÂàÀëËçæà' COLLATE Latin1_General_CS_AS   
SELECT N'éÉâÂàÀëËçæà' COLLATE Latin1_General_CI_AI
go

Output:

-----------
eEaAaAeEc?a

(1 row(s) affected)


-----------
eEaAaAeEc?a

(1 row(s) affected)


-----------
éÉâÂàÀëËçæà

(1 row(s) affected)


-----------
éÉâÂàÀëËçæà

(1 row(s) affected)



回答2:


According to this character table Cyrillic_General_CI_AS doesn't contain the æ chacter. This would explain why you see a ? in it's place if you are truly using the defaults instead of what you have in your select statements.



来源:https://stackoverflow.com/questions/4211750/why-doesnt-explicit-collate-override-database-collation

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