SQL server SORT order does not correspond to ASCII code order

前端 未结 3 1664
野趣味
野趣味 2021-01-18 11:53

I\'m using SQL Server 2012, and I have a database with a SQL_Latin1_General_CP1_CI_AS collation:

create table testtable (c nvarchar(1) null)

i         


        
3条回答
  •  一整个雨季
    2021-01-18 12:29

    Thanks Ross et al! Found the following documentation which has an excellent explanation on MS SQL Server collation, thought I'd post it here so as to benefit those who comes across this question or related questions.


    Collation A collation specifies the bit patterns that represent each character in a data set. Collations also determine the rules that sort and compare data. SQL Server supports storing objects that have different collations in a single database. For non-Unicode columns, the collation setting specifies the code page for the data and which characters can be represented. Data that is moved between non-Unicode columns must be converted from the source code page to the destination code page.

    For more details read it here http://goo.gl/RpBGWN


    From my code snippet, if I wanted to sort the value in a binary order, the query can be changed to the following: select c, ASCII(c) ascvalue from testtable order by c collate Latin1_General_BIN

    Or change collation definition when creating the table create table testtable (c nvarchar(1) collate Latin1_General_BIN null)

    Which yields the following result:

    c | ascvalue
    ------------
    8 | 56
    9 | 57
    : | 58
    ; | 59
    

提交回复
热议问题