【问题标题】:Why are HashBytes differentWhy are HashBytes different
【发布时间】:2022-12-02 06:33:57
【问题描述】:

When executing that query:

SELECT HashBytes('MD5', CONCAT('AVS',convert(nvarchar,313),'@310001')) as mycol ...

I get HashBytes represented such as

0xA6A0D....

When I do not convert, the HashBytes look like the following:

SELECT HashBytes('MD5', CONCAT('AVS','313','@310001')) as mycol

0x88230...

Why is it different?

【问题讨论】:

  • N'313' and '313' aren't the same value, and therefore, neither are N'AVS313@310001' and 'AVS313@310001' and so you get different hash values. Data typing is important.

标签: sql-server


【解决方案1】:

As mentioned by Larnu in the cmets, data types matter. To see why you're getting different hashes lets take a look at what CONCAT outputs for the two cases:

SELECT CAST(CONCAT('AVS',convert(nvarchar,313),'@310001') AS VARBINARY(MAX));
0x4100560053003300310033004000330031003000300030003100
SELECT CAST(CONCAT('AVS','313','@310001') AS VARBINARY(MAX));
0x41565333313340333130303031

Because one of the parameters to CONCAT is of type nvarchar the others are coerced to nvarchar as well to return an nvarchar result.

Read through theRemarkssection of the CONCAT (Transact-SQL) documentation for a complete accounting of the expected output types for given input types.

【讨论】:

    猜你喜欢
    • 2021-01-15
    • 2022-12-02
    • 2023-03-15
    • 2022-12-01
    • 2022-01-29
    • 2015-05-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多