请问在sqlserver中如何将binary类型查询结果转换为varchar类型??(100分)

L

lycok

Unregistered / Unconfirmed
GUEST, unregistred user!
我想在sqlserver中将binary类型查询结果转换为varchar类型
如下:
declare @id varbinary(2),@ccc varchar(4)
select @id=0xAABB
那么我如何将@id 的值作为字符'AABB'赋给@ccc呢?我知道这些在delphi很容易实现,但是
现在是要在sqlserver的存储过程中实现,该怎么办呢?请数据库高手指教
 
gz是什么意思?你能说详细点吗?谢谢
 
select @ccc=cast(@id as varchar(4))

GZ是关注
 
select @ccc=cast(@id as varchar(4)) 我试过了,这样不行啊
 
你在查询分析器里执行一下,41转为10进制为65,就是字符A

declare @id varbinary(4),@ccc varchar(4)
select @id=0x41414141
select @ccc=cast(@id as varchar(4))
select @ccc
GO
 
结果是'AAAA',但是我要的不是这种结果呀,我是要@id是什么内容,@ccc就是什么内容
如 @id=0x41414141 那么@ccc就应该是'41414141'
 
declare @id varbinary(2),@ccc varchar(4)
declare @int1 integer,@int2 integer,@int3 integer,@int4 integer
set @id=0xAABB
set @int1 = (@id & cast(0xF000 as integer))/(16*16*16)
set @int2 = (@id & cast(0x0F00 as integer))/(16*16)
set @int3 = (@id & cast(0x00F0 as integer))/16
set @int4 = (@id & cast(0x000F as integer))

if @int1 > 9 set @int1 = @int1 + (65 - 10) else set @int1 = @int1 + 48
if @int2 > 9 set @int2 = @int2 + (65 - 10) else set @int2 = @int2 + 48
if @int3 > 9 set @int3 = @int3 + (65 - 10) else set @int3 = @int3 + 48
if @int4 > 9 set @int4 = @int4 + (65 - 10) else set @int4 = @int4 + 48
set @ccc = char(@int1) + char(@int2) + char(@int3) + char(@int4)
select @ccc
GO
 
非常感谢QuickSilver,我还能问一下,set这个语句是起什么作用的吗?
 
set @int1 = (@id & cast(0xF000 as integer))/(16*16*16)
set @int2 = (@id & cast(0x0F00 as integer))/(16*16)
set @int3 = (@id & cast(0x00F0 as integer))/16
set @int4 = (@id & cast(0x000F as integer))
不好意思,我还想问一下,这四句是起什么作用的?
 
非常感谢,分全是你的啦:)
 
顶部