0

闲置代码

declare @b varbinary(8) = 1
select cast(@b as varchar(max)), @b

返回

铸造原件
        0x00000001

如何将时间戳/varbinary 值转换为 varchar?应为“0x00000001”。

4

2 回答 2

1

试试这个,它适用于你的场景

select convert(varchar, @b, 1), @b   // <---- 0x00000001

这可以帮助您:将二进制值转换为字符串值

于 2013-07-11T22:35:27.703 回答
0

我不知道这是否是您正在寻找的,但是我在将 binary(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 有帮助

于 2013-07-12T07:17:34.333 回答