I am using code like this in my SELECT statement:
CAST(HASHBYTES(N'SHA1', Bla) AS NVARCHAR(MAX)) AS hashed_bla
and end-up with "chinese"/UTF encoded characters in the ssms grid but also in upstream apps. Is there a way to change this? Does this have to do with the collation? Thanks!
What you have is working as expected. Take the following example:
SELECT HASHBYTES('SHA1','B8187F0D-5DBA-4D43-95FC-CD5A009DB98C');
This returns the varbinary
value 0xA04B9CB18A2DC4BC08B83FCCE48A0AF1A1390756
. You are then converting that value to an nvarchar
, so get a result like N'䮠놜ⶊ별레찿諤㦡嘇'
(on my collation). For an varbinary
each 4 characters represents a single character. So, for the above A04B
is the first character (which is N'䮠'
).
It appears what you are after is an varchar
representing a varbinary
value (you don't need an nvarchar
here, as there will be no unicode characters). To do so, you need to use CONVERT
and a style code. For the example I gave above that would be:
SELECT CONVERT(varchar(100),HASHBYTES('SHA1','B8187F0D-5DBA-4D43-95FC-CD5A009DB98C'),1);
Which returns the varchar
value '0xA04B9CB18A2DC4BC08B83FCCE48A0AF1A1390756'
. If you don't want the '0x'
at the start, use style code 2
, rather than 1
.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.