Last byte missing when casting from varbinary to varchar
- by xaxie
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 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!