Checksum function is depended on Unicode?
if its in unicode so the results : ( notice the N )
select CHECKSUM(N'2Volvo Director 20') ---341465450
select CHECKSUM(N'3Volvo Director 30') ---341453853
select CHECKSUM(N'4开发者_开发百科Volvo Director 40') ---341455363
but if its regular :
select CHECKSUM('2Volvo Director 20') ---1757834048
select CHECKSUM('3Volvo Director 30') ---1757834048
select CHECKSUM('4Volvo Director 40') ---1757834048
Can you please explain me why in the first situation - it gives me different and in the second it gives me the same ?
there is a lead article about it which says :
However the CHECKSUM() function evaluates the type as well as compares the two strings and if they are equal, then only the same value is returned.
This seems to be collation dependant.
DECLARE @T TABLE
(
SQL_Latin1_General_CP1255_CI_AS varchar(100) COLLATE SQL_Latin1_General_CP1255_CI_AS,
Latin1_General_CI_AS varchar(100) COLLATE Latin1_General_CI_AS
)
INSERT INTO @T
SELECT '2Volvo Director 20','2Volvo Director 20' UNION ALL
SELECT '3Volvo Director 30','3Volvo Director 30' UNION ALL
SELECT '4Volvo Director 40','4Volvo Director 40' UNION ALL
SELECT '5Volvo Director 50','5Volvo Director 50' UNION ALL
SELECT '6Volvo Director 60','6Volvo Director 60'
SELECT
CHECKSUM(SQL_Latin1_General_CP1255_CI_AS) AS SQL_Latin1_General_CP1255_CI_AS,
CHECKSUM(Latin1_General_CI_AS) AS Latin1_General_CI_AS
FROM @T
Returns
SQL_Latin1_General_CP1255_CI_A Latin1_General_CI_AS
------------------------------ --------------------
-1757834048 -341465450
-1757834048 -341453853
-1757834048 -341455363
-1757834048 -341442609
-1757834048 -341448488
CHECKSUM
is documented as being more collision prone than HashBytes
. I'm not sure specifically why the CP
collation has this behaviour for these inputs though.
精彩评论