Last byte missing when casting from varbinary to varchar
I got the last byte losing when converting varbinary to varchar in some case. For example:
DECLARE @binary varbinary(8000),
@char varchar(8000)
set @binary = 0x000082
set @char = CAST(@binary as varchar(8000))
select BinaryLength=DATALENGTH(@binary), CharLength=DATALENGTH(@char)
The result is
BinaryLength CharLength
3 2
The affected byte value开发者_StackOverflow中文版 is from 0x81 - 0xFE.
The stranger thing is that if I use varchar(MAX) instead varchar(8000) when casting, there is no any problem.
Could someone tell me the root cause of the issue?
PS: I run the sql in MS SQL server 2008.
Thanks!
精彩评论