Choosing a binary collation that can differentiate between 'ss' and 'ß' for nvarchar column in Sql Server

前端 未结 2 848
庸人自扰
庸人自扰 2020-12-30 05:36

As the default SQL_Latin1_General_CP1_CI_AS collation of SQL server can\'t differentiate between ss and ß, I want to change the collat

2条回答
  •  南笙
    南笙 (楼主)
    2020-12-30 05:41

    In general, BIN2 would be preferable over BIN, and you may want to choose a windows collation over a sql collation. e.g. Latin1_General_100_BIN2

    Guidelines for Using BIN and BIN2 Collations

    Guidelines for Using BIN Collations

    If your SQL Server applications interact with older versions of SQL Server that use binary collations, continue to use binary. Binary collations might be a more suitable choice for mixed environments.


    For similar reasons to what has just been stated regarding the BIN2 collations, unless you have specific requirements to maintain backwards-compatibility behavior, you should lean towards using the Windows collations and not the SQL Server-specific collations (i.e. the ones starting with SQL are now considered kinda "sucky" ;-) ).
    - @srutzky - Latin1_General_BIN performance impact when changing the database default collation


    rextester demo: http://rextester.com/KIIDYH74471

    create table t (
        a varchar(16)  --collate SQL_Latin1_General_CP1_CI_AS /* default */
      , b varchar(16)  --collate SQL_Latin1_General_CP1_CI_AS
      , c nvarchar(16) --collate SQL_Latin1_General_CP1_CI_AS
      , d nvarchar(16) --collate SQL_Latin1_General_CP1_CI_AS 
    );
    insert into t values ('ss','ß',N'ss',N'ß');
    select *
        , case when a = b then '=' else '!=' end as [a=b] /* != */
        , case when a = d then '=' else '!=' end as [a=d] /* = */
        , case when c = b then '=' else '!=' end as [c=b] /* = */
        , case when c = d then '=' else '!=' end as [c=d] /* = */
    from t;
    

    returns:

    +----+---+----+---+-----+-----+-----+-----+
    | a  | b | c  | d | a=b | a=d | c=b | c=d |
    +----+---+----+---+-----+-----+-----+-----+
    | ss | ß | ss | ß | !=  | =   | =   | =   |
    +----+---+----+---+-----+-----+-----+-----+
    

    create table t (
        a varchar(16)  collate Latin1_General_100_BIN2
      , b varchar(16)  collate Latin1_General_100_BIN2
      , c nvarchar(16) collate Latin1_General_100_BIN2
      , d nvarchar(16) collate Latin1_General_100_BIN2
    );
    insert into t values ('ss','ß',N'ss',N'ß');
    select *
        , case when a = b then '=' else '!=' end as [a=b] /* != */
        , case when a = d then '=' else '!=' end as [a=d] /* != */
        , case when c = b then '=' else '!=' end as [c=b] /* != */
        , case when c = d then '=' else '!=' end as [c=d] /* != */
    from t;
    

    returns:

    +----+---+----+---+-----+-----+-----+-----+
    | a  | b | c  | d | a=b | a=d | c=b | c=d |
    +----+---+----+---+-----+-----+-----+-----+
    | ss | ß | ss | ß | !=  | !=  | !=  | !=  |
    +----+---+----+---+-----+-----+-----+-----+
    

提交回复
热议问题