以下代码
declare @b varbinary(8) = 1
select cast(@b as varchar(max)), @b
返回
Casted Original 0x00000001
如何将timestamp / varbinary值转换为varchar?预计“0x00000001”。
答案 0 :(得分:1)
试试这个,它适用于你的场景
select convert(varchar, @b, 1), @b // <---- 0x00000001
答案 1 :(得分:0)
我不知道这是不是你要找的东西,但我在转换为/从二进制(8)到varchar时遇到了很多麻烦,请看下面的函数是否转换。
declare @binary binary(8)
declare @radhe varchar(100) -- this is just a variable to store the returning value at the end
select @binary = 0x000000000000037A
;with ctea as
(
select maxlen = MAX(len(@binary))
) ,
cte as
(
select i = 1
union all
select i = i + 1 from cte,ctea where i < maxlen
) ,
cte2 as
(
select i, j=convert(int,SUBSTRING(@binary,i,1)) from cte
),
cte3 as
(
select i, j=j/16, k=j%16 from cte2
),
ctehex as
(
select i
,j
,k
,h1=case when j<10 then convert(varchar(1),j)
else CHAR(55+j)
end
,h2=case when k<10 then convert(varchar(1),k)
else CHAR(55+k)
end
from cte3
)
select @radhe = '0x'+(select h1+h2 from ctehex for xml path (''))
select @radhe as [the varchar value], @binary as [the binary(8) value]
- 希望这有助于M