leanhluudan
leanhluudan

Reputation: 63

Why are 𦍌 and 𧾷 are the same in SQL Server

In SSMS v17.9, connecting to an SQL Server 14.0.1000 instance, the system treats these two character the same: 𦍌 and 𧾷

I set a [Kanji] table with the [Char] NVARCHAR(2) as the primary key. After I added "𦍌", I cannot add "𧾷" since it threw a "key duplication" error.

I ran this T-SQL:

IF ( N'𦍌' = N'𧾷') PRINT 'true' ELSE PRINT 'false'

The result prints out 'true'.

Upvotes: 5

Views: 126

Answers (3)

Hiran
Hiran

Reputation: 1180

you have to use proper collation,

IF ( N'𦍌' COLLATE Chinese_Simplified_Stroke_Order_100_CI_AI = N'𧾷' COLLATE Chinese_Simplified_Stroke_Order_100_CI_AI) PRINT 'true' ELSE PRINT 'false'

collation can set on the fly in select statement or in DB object level

read more about collations

Upvotes: 6

Gottfried Lesigang
Gottfried Lesigang

Reputation: 67311

Besides the collations I'd suggest to have a look into the unicode code points or the binary representation:

SELECT UNICODE(N'𦍌'), CAST(N'𦍌' AS BINARY(4))
     , UNICODE(N'𧾷'), CAST(N'𧾷' AS BINARY(4))

The result

55384   0x58D84CDF  
55391   0x5FD8B7DF

Upvotes: 2

Vahid Farahmandian
Vahid Farahmandian

Reputation: 6566

Based on a definition from Wikipedia: https://en.wikipedia.org/wiki/Collation

A collation algorithm such as the Unicode collation algorithm defines an order through the process of comparing two given character strings and deciding which should come before the other. When an order has been defined in this way, a sorting algorithm can be used to put a list of any number of items into that order.

When you refer to a character, that SQL Server does not have any scene about it, then SQL Server will treat it as ? character, therefor you will see that both 𦍌 and 𧾷 characters are same, because both are treated as ?

To prevent such a behaviour, you need to configure suitable collation on your database or table column or string literal.

So as @Hiran posted(https://stackoverflow.com/a/56220111/1666800) you will need to configure suitable collation.

Read more about collation in SQL Server: https://learn.microsoft.com/en-us/sql/t-sql/statements/collations?view=sql-server-2017

Here are some codes which will help you to configure the collation:

Set Collation on database:

ALTER DATABASE <Database>
COLLATE <Your Collation> ;
GO

Set Collation on Table Column:

USE <Database>
GO
ALTER TABLE <Table Nme>
ALTER COLUMN <Column Name> <Data Type>
COLLATE <Your Collation> <Other options>
GO

Set Collation on a string literal:

SELECT N'𦍌' COLLATE <Your Collation>

Upvotes: 3

Related Questions