开发者

Last byte missing when casting from varbinary to varchar

开发者 https://www.devze.com 2022-12-25 23:17 出处:网络
I got the last byte losing when converting varbinary to varchar in some case. For example: DECLARE @binary varbinary(8000),

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!

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号